2

After half a day googling I have managed to register a custom HttpMessageConverter over the configuration class. Like this:

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

 @Bean
 @Override
 public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
    RequestMappingHandlerAdapter handlerAdapter = super.requestMappingHandlerAdapter();
    handlerAdapter.getMessageConverters().add(0, new CustomConverter());
    return handlerAdapter;
 }
}

Is there a way to do it using XML configuration file?

tereško
  • 58,060
  • 25
  • 98
  • 150
30thh
  • 10,861
  • 6
  • 32
  • 42
  • 2
    http://stackoverflow.com/questions/5019162/custom-httpmessageconverter-with-responsebody-to-do-json-things – mvb13 Nov 18 '13 at 09:09

1 Answers1

4

Here is the answer for Spring 3.2:

<mvc:annotation-driven >
    <mvc:message-converters register-defaults="false">
        <bean class="me.MyCustomMessageConverter"/>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="writeAcceptCharset" value="false"/>
        </bean>
   </mvc:message-converters>
</mvc:annotation-driven>

Do not forget to define the mvc namespace: http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

I did not find, how to add the own converter on the top on the converters list. It is possible to redefine the converters stack using register-defaults attribute.

Standard stack is defined in the constructor of this Spring class RequestMappingHandlerAdapter. One can copy needed converters from there.

30thh
  • 10,861
  • 6
  • 32
  • 42