Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

@Configuration
public class MdxOnDemandSerializationWebMvcConfigurer implements WebMvcConfigurer {

/**
* List of all configured converts. All others will be removed before inserting custom MappingJackson2XmlHttpMessageConverter
*/
Expand All @@ -32,9 +33,8 @@ public class MdxOnDemandSerializationWebMvcConfigurer implements WebMvcConfigure
}

@Override
public final void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// Get rid of unused converters
// These get in the way of the other
public final void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// Filter out unwanted default converters to strictly enforce custom serialization
List<HttpMessageConverter<?>> toRemove = converters.stream().filter(t -> !CONVERT_CLASSES.contains(t.getClass())).collect(Collectors.toCollection(ArrayList::new));
converters.removeAll(toRemove);

Expand Down
Original file line number Diff line number Diff line change
@@ -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<HttpMessageConverter<?>> converters) {
GsonHttpMessageConverter gsonConverter = new GsonHttpMessageConverter();
gsonConverter.setGson(gson());
converters.add(gsonConverter);
return new MdxSerializerFactoryBean().getObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MdxOnDemandSerializationWebMvcConfigurerTest extends Specification {
]

when:
subject.configureMessageConverters(converters)
subject.extendMessageConverters(converters)

then:
verifyAll(converters) {
Expand Down
Loading