Spring Boot - Managing HttpMessageConverters

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

HttpMessageConverters provides a convenient way to manage HttpMessageConverters to a web application. It can be registered as a bean. The following public constructors of this class can be used:

 HttpMessageConverters(HttpMessageConverter<?>... additionalConverters)
 HttpMessageConverters(Collection<HttpMessageConverter<?>> additionalConverters)
HttpMessageConverters(boolean addDefaultConverters, Collection<HttpMessageConverter<?>> converters)

Example

Using HttpMessageConverters

Adding additional converters

@SpringBootApplication
public class AppMain {
   @Bean
   public HttpMessageConverters additionalConverters() {
       return new HttpMessageConverters(new TheCustomConverter());
   }
}
@Component
public class RefreshListener {
  @Autowired
  private RequestMappingHandlerAdapter handlerAdapter;

  @EventListener
  public void handleContextRefresh(ContextRefreshedEvent event) {
      handlerAdapter.getMessageConverters()
                    .stream()
                    .forEach(System.out::println);
  }
}

Output

Running the main class:

org.springframework.http.converter.ByteArrayHttpMessageConverter@41838c01
com.logicbig.example.TheCustomConverter@7ce24b48
org.springframework.http.converter.StringHttpMessageConverter@69455568
org.springframework.http.converter.ResourceHttpMessageConverter@4997399d
org.springframework.http.converter.xml.SourceHttpMessageConverter@1429472a
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@7affe6ec
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@39d202ce
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@49f98fb8

Replacing default converters

@SpringBootApplication
public class AppMain {
  @Bean
  public HttpMessageConverters converters() {
      return new HttpMessageConverters(
              false, Arrays.asList(new TheCustomConverter()));
  }
}

Output

com.logicbig.example.TheCustomConverter@305e1d50

Registering HttpMessageConverter as Bean

An HttpMessageConverter can also be registered as a bean directly:

@SpringBootApplication
public class AppMain {
   @Bean
   public HttpMessageConverter<?> messageConverter(){
       return new TheCustomConverter();
   }
}

It will produce the same output as the last one.

To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run

Also checkout how to register the converters without Spring boot here.

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter-web 1.5.2.RELEASE: Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
    Corresponding Spring version: 4.3.7.RELEASE
  • JDK 1.8
  • Maven 3.3.9

HttpMessageConverters Registration Select All Download
  • message-converters
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also