Spring MVC - List of default registered HttpMessageConverter

[Updated: Sep 3, 2017, Created: Sep 3, 2017]

The registered HttpMessageConverter list is normally used at two places: RequestMappingHandlerAdapter (tutorial here) and HandlerExceptionResolvers (tutorial here). The list is same at both places. This can be confirmed by finding WebMvcConfigurationSupport#getMessageConverters()method references in your IDE. Following screenshot is from Intellij:

In this quick example, we will see how to print list of all registered converters.


@EnableWebMvc
public class AppConfig {

  @Autowired
  private RequestMappingHandlerAdapter handlerAdapter;

  @EventListener
  public void handleContextRefresh(ContextRefreshedEvent event) {
      System.out.println("-- context refreshed --");
      System.out.println("context: "+
              event.getApplicationContext());

      handlerAdapter.getMessageConverters()
                    .stream()
                    .forEach(System.out::println);
      System.out.println("-------");
  }
}

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

Output

On the server console:

........
Sep 03, 2017 4:58:27 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
INFO: Registering annotated classes: [class com.logicbig.example.AppConfig]
Sep 03, 2017 4:58:27 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Sep 03, 2017 4:58:27 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Sun Sep 03 16:58:27 CDT 2017]; root of context hierarchy
Sep 03, 2017 4:58:27 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 605 ms
-- context refreshed --
context: Root WebApplicationContext: startup date [Sun Sep 03 19:52:09 CDT 2017]; root of context hierarchy
org.springframework.http.converter.ByteArrayHttpMessageConverter@430df3a
org.springframework.http.converter.StringHttpMessageConverter@4e86f4d1
org.springframework.http.converter.ResourceHttpMessageConverter@2618a56e
org.springframework.http.converter.xml.SourceHttpMessageConverter@7b394fa2
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@797296f0
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@2bc573
-------
Sep 03, 2017 4:58:27 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Sep 03, 2017 4:58:27 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Sep 03, 2017 4:58:27 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Sep 03 16:58:27 CDT 2017]; parent: Root WebApplicationContext
Sep 03, 2017 4:58:27 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Sep 03, 2017 4:58:27 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 23 ms
-- context refreshed --
context: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Sep 03 19:54:29 CDT 2017]; parent: Root WebApplicationContext
org.springframework.http.converter.ByteArrayHttpMessageConverter@1679a1a1
org.springframework.http.converter.StringHttpMessageConverter@1543c079
org.springframework.http.converter.ResourceHttpMessageConverter@5fc320c0
org.springframework.http.converter.xml.SourceHttpMessageConverter@1df56a71
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@46daa4c7
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@4d91f46d
-------
Sep 03, 2017 4:58:27 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]

The list is printed twice on two parent/child contexts refresh events.

The list might be different from version to version, also there might be more converters depending on the dependencies on the classpath and other application level custom converters. The next tutorial shows how to register custom converters.

Example Project

Dependencies and Technologies Used :

  • spring-webmvc 4.2.4.RELEASE: Spring Web MVC.
  • spring-test 4.2.4.RELEASE: Spring TestContext Framework.
  • javax.servlet-api 3.0.1 Java Servlet API
  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.3.9

Printing List of default HttpMessageConverter Select All Download
  • list-of-message-converter
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also