Is there a way to get all registered message converters?

I would like to somehow introduce all the HttpMessageConverter instances registered in Spring-MVC. I can successfully implement everything that has been registered through.

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

However, this only introduces if the converter was registered within the context (i.e. if it is defined outside of <annotation-driven> ).

I made a drawing, I would try using <beans:ref inside <annotation-driven><message-converters> , but it is not supported in Spring -web 3.1.

Is there any class that I can add that might have a property that I could use to get the converters? Ideally, I would like to see that the order in the filter chain is also registered.

+4
source share
3 answers

You are right, message converters are directly created in the RequestMappingHandlerAdapter registered with the <mvc:annotation-driven/> xml <mvc:annotation-driven/> , and message subtag converters explicitly assume that the bean will be defined in the string.

However, a workaround is to identify the processor adapter and inject it into the transducers as follows:

 <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> 
+5
source

Spring puts all converters behind the implementation of org.springframework.core.convert.ConversionService . You need to inject an instance of this interface into your class, you can read more in the spring documentation (including an example of how to introduce it).

+4
source

You can try introducing a bean of type RequestMappingHandlerAdapter , but depending on your configuration, you may not have an instance!

0
source

All Articles