Skip to content
Closed
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 @@ -11,6 +11,7 @@
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
import org.springframework.http.converter.xml.JacksonXmlHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

Expand All @@ -22,14 +23,18 @@
public class MdxOnDemandSerializationWebMvcConfigurer implements WebMvcConfigurer {

/**
* List of all configured converts. All others will be removed before inserting custom MappingJackson2XmlHttpMessageConverter
* Converters retained by {@link #extendMessageConverters(List)}; every other converter is removed before the
* custom XML converter is appended. Gson handles MDX JSON serialization. The Jackson JSON converter is retained
* because Spring Boot's actuator endpoints (e.g. {@code /actuator/health}) are serialized with Jackson; without it
* those responses have no writable converter and fail with {@code HttpMessageNotWritableException}.
*/
static final List<Class<?>> CONVERT_CLASSES;
static {
CONVERT_CLASSES = new ArrayList<>();
CONVERT_CLASSES.add(GsonHttpMessageConverter.class);
CONVERT_CLASSES.add(StringHttpMessageConverter.class);
CONVERT_CLASSES.add(ByteArrayHttpMessageConverter.class);
CONVERT_CLASSES.add(JacksonJsonHttpMessageConverter.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.springframework.http.converter.FormHttpMessageConverter
import org.springframework.http.converter.ResourceHttpMessageConverter
import org.springframework.http.converter.StringHttpMessageConverter
import org.springframework.http.converter.json.GsonHttpMessageConverter
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter
import org.springframework.http.converter.xml.JacksonXmlHttpMessageConverter

import spock.lang.Specification
Expand All @@ -22,6 +23,7 @@ class MdxOnDemandSerializationWebMvcConfigurerTest extends Specification {
def "filters non-whitelisted message converters and appends the custom XML converter"() {
given:
def gsonConverter = new GsonHttpMessageConverter()
def jacksonJsonConverter = new JacksonJsonHttpMessageConverter()
def stringConverter = new StringHttpMessageConverter()
def byteArrayConverter = new ByteArrayHttpMessageConverter()
def unwantedResourceConverter = new ResourceHttpMessageConverter()
Expand All @@ -30,6 +32,7 @@ class MdxOnDemandSerializationWebMvcConfigurerTest extends Specification {
def converters = [
unwantedResourceConverter,
gsonConverter,
jacksonJsonConverter,
unwantedFormConverter,
stringConverter,
byteArrayConverter
Expand All @@ -40,8 +43,9 @@ class MdxOnDemandSerializationWebMvcConfigurerTest extends Specification {

then:
verifyAll(converters) {
it.size() == 4
it.size() == 5
it.contains(gsonConverter)
it.contains(jacksonJsonConverter)
it.contains(stringConverter)
it.contains(byteArrayConverter)
!it.contains(unwantedResourceConverter)
Expand Down
Loading