I'm trying to understand why my spring v.5.0.4-RELEASE
doesn't load correctly the default message converters.
I removed all declaration from my servlet.xml and I was expecting to find all the default converters correctly loaded from AbstractMessageConverterMethodProcessor
inside spring, but I get only the following 4:
org.springframework.http.converter.ByteArrayHttpMessageConverter@35ca138b
org.springframework.http.converter.StringHttpMessageConverter@2b755f0d
org.springframework.http.converter.xml.SourceHttpMessageConverter@74f5d717
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@6982b849
Any clue on this?
Spring 3.0 introduces a simple Converter interface that you can implement and reuse anywhere in Spring. You can use them in Spring MVC to convert request String values to Controller method parameter values of any Object type that you can write a Converter for.
If Jackson is on the classpath, any controller in Spring applications renders the JSON response by default.
Http Message Converter is responsible for serializing Java Object to JSON/XML data representation and deserializing JSON/XML data representation to Java Object.
MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath) MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath) AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
I ended up understanding the issue was caused by the RequestMappingHandlerAdapter
bean
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
This was overriding the Spring defaults and publishing the four converters listed in my question. Solution was to place the converters I was looking for, under the bean like follows:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="autoDetectFields" value="true" />
<property name="autoDetectGettersSetters" value="false" />
<property name="objectMapper">
<bean class="com.mypackage.CustomMapper" />
</property>
</bean>
</property>
</bean>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
The configuration placed under the annotation-driven
was totally ignored:
<mvc:annotation-driven>
<mvc:message-converters>
...
</mvc:message-converters>
</mvc:annotation-driven>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With