diff --git a/mdx-web/src/main/java/com/mx/path/model/mdx/web/MdxOnDemandSerializationWebMvcConfigurer.java b/mdx-web/src/main/java/com/mx/path/model/mdx/web/MdxOnDemandSerializationWebMvcConfigurer.java index 2f42f442..75ca5475 100644 --- a/mdx-web/src/main/java/com/mx/path/model/mdx/web/MdxOnDemandSerializationWebMvcConfigurer.java +++ b/mdx-web/src/main/java/com/mx/path/model/mdx/web/MdxOnDemandSerializationWebMvcConfigurer.java @@ -20,6 +20,7 @@ @Configuration public class MdxOnDemandSerializationWebMvcConfigurer implements WebMvcConfigurer { + /** * List of all configured converts. All others will be removed before inserting custom MappingJackson2XmlHttpMessageConverter */ @@ -32,9 +33,8 @@ public class MdxOnDemandSerializationWebMvcConfigurer implements WebMvcConfigure } @Override - public final void configureMessageConverters(List> converters) { - // Get rid of unused converters - // These get in the way of the other + public final void extendMessageConverters(List> converters) { + // Filter out unwanted default converters to strictly enforce custom serialization List> toRemove = converters.stream().filter(t -> !CONVERT_CLASSES.contains(t.getClass())).collect(Collectors.toCollection(ArrayList::new)); converters.removeAll(toRemove); diff --git a/mdx-web/src/main/java/com/mx/path/model/mdx/web/MdxSerializationConfiguration.java b/mdx-web/src/main/java/com/mx/path/model/mdx/web/MdxSerializationConfiguration.java index 4d100a64..a08f79f8 100644 --- a/mdx-web/src/main/java/com/mx/path/model/mdx/web/MdxSerializationConfiguration.java +++ b/mdx-web/src/main/java/com/mx/path/model/mdx/web/MdxSerializationConfiguration.java @@ -1,32 +1,22 @@ package com.mx.path.model.mdx.web; -import java.util.List; - import com.google.gson.Gson; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.http.converter.json.GsonHttpMessageConverter; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration -@SuppressWarnings("checkstyle:designforextension") -public class MdxSerializationConfiguration implements WebMvcConfigurer { - public MdxSerializerFactoryBean gsonBeanFactory() { - MdxSerializerFactoryBean factory = new MdxSerializerFactoryBean(); - return factory; - } +public class MdxSerializationConfiguration { + /** + * Provides the customized Gson instance for the application. + * Spring Boot automatically detects this Bean and uses it to configure + * the default GsonHttpMessageConverter for all JSON serialization. + * + * @return a fully configured Gson instance + */ @Bean public Gson gson() { - return gsonBeanFactory().getObject(); - } - - @Override - public void configureMessageConverters(List> converters) { - GsonHttpMessageConverter gsonConverter = new GsonHttpMessageConverter(); - gsonConverter.setGson(gson()); - converters.add(gsonConverter); + return new MdxSerializerFactoryBean().getObject(); } } diff --git a/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/MdxOnDemandSerializationWebMvcConfigurerTest.groovy b/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/MdxOnDemandSerializationWebMvcConfigurerTest.groovy index 36c4f66f..25c18327 100644 --- a/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/MdxOnDemandSerializationWebMvcConfigurerTest.groovy +++ b/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/MdxOnDemandSerializationWebMvcConfigurerTest.groovy @@ -36,7 +36,7 @@ class MdxOnDemandSerializationWebMvcConfigurerTest extends Specification { ] when: - subject.configureMessageConverters(converters) + subject.extendMessageConverters(converters) then: verifyAll(converters) {