Brett Ryan Brett Ryan - 9 days ago 6
Java Question

Is there a way to get all registered message-converters?

I would like to somehow inject all

HttpMessageConverter
instances registered in Spring-MVC. I can successfully inject all that have been registered via.

private HttpMessageConverter[] converters;
@Autowired
public void setConverters(HttpMessageConverter[] converters) {
this.converters = converters;
}


However this only injects if the converter was registered inside the context (i.e. if defined outside of
<annotation-driven>
).

I did figure I would try using
<beans:ref
inside the
<annotation-driven><message-converters>
but it is not supported in spring-web 3.1.

Is there some class I can inject that may have a property I could use to get converters? Ideally I'd like to see the order in the filter chain they are registered in too.

Answer

You are right, the message converters are directly instantiated within the RequestMappingHandlerAdapter registered using the <mvc:annotation-driven/> xml tag, and the message-converters subtag explicitly expect the bean to be defined inline.

However, a workaround is to define the handler adapter and inject in the converters this way:

<bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="conversionService" ref="conversionService"></property>
            <property name="validator">
                <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
                    <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
                </bean>
            </property>
        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <ref bean="byteArrayConverter"/>
            <ref bean="jaxbConverter"/>
            <ref bean="jsonConverter"/>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
            <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
        </list>
    </property>
</bean>

<bean name="byteArrayConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean name="jaxbConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean name="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>

<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useSuffixPatternMatch" value="false"></property>
</bean>
Comments