Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Message Converters in Spring MVC 5

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?

like image 233
Lorenzo Sciuto Avatar asked Mar 07 '18 12:03

Lorenzo Sciuto


People also ask

What are converters in Spring MVC?

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.

What is the default response type in spring?

If Jackson is on the classpath, any controller in Spring applications renders the JSON response by default.

What is an HTTP message converter in spring rest?

Http Message Converter is responsible for serializing Java Object to JSON/XML data representation and deserializing JSON/XML data representation to Java Object.

What is the use of MappingJackson2HttpMessageConverter?

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)


1 Answers

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>
like image 93
Lorenzo Sciuto Avatar answered Sep 30 '22 13:09

Lorenzo Sciuto