How to customize HTTP message converters in Spring Boot project

In building the RESTful data service, we defined Controller, Repositories, and some annotations, but so far we have not performed object transformation, converting Java entity objects into HTTP data output streams.

The Spring Boot bottom layer outputs Java entity classes in JSON format via HttpMessageConverters and Jackson libraries. When multiple converters are available, choose the most appropriate converter to use based on the message object type and the required content type.

In SpringMVC source Code analysis (5)- Message converter HttpMessageConverter article, there is a diagram that can clearly show the location of the message converter.

The goals of the message converter are:

  1. Conversion of HTTP input request formats to Java objects;
  2. Translation of a Java object to an HTTP output request.

Some message converters support only multiple data types, some support only multiple output formats, and some support both.

Such as: MappingJackson2HttpMessageConverter can convert Java objects to application/json, While ProtobufHttpMessageConverter only supports com. Google. Protobuf. Message types of input, But you can output application/json, application/ XML, Text /plain, and Application/X-protobuf formats.

practice

There are three ways to configure message converters in a project, measured primarily in terms of customizability and ease of use.

  1. Add the @Bean definition to the WebConfiguration class
@Bean public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { return new ByteArrayHttpMessageConverter();  }Copy the code

  1. OverrideconfigureMessageConvertersMethod to extend the existing list of message converters;
@Override public void configureMessageConverters(List<HttpMessageConverter<? >> converters) { converters.add(new ByteArrayHttpMessageConverter()); }Copy the code

  1. More control can be rewrittenextendMessageConvertersMethod, first empty the converter list, and then add the custom converter.
@Override public void extendMessageConverters(List<HttpMessageConverter<? >> converters) { converters.clear(); converters.add(new ByteArrayHttpMessageConverter()); }Copy the code

Analysis of the

Spring Boot provides multiple ways to accomplish the same task, depending on whether we’re more focused on convenience or customization.

What are the differences between the three methods mentioned above?

Defining HttpMessageConverter via @bean is the easiest way to add message converters to your project, similar to adding Servlet Filters mentioned earlier. If Spring scans a bean of type HttpMessageConverter, it is automatically added to the invocation chain. It is recommended that the WebConfiguration in your project inherit from the WebMvcConfigurerAdapter.

Through rewriting configureMessageConverters method to add custom converter is very convenient, but there is a weakness: If the project in multiple WebMvcConfigurers instance (our own definition, or Spring Boot default), does not ensure rewritten configureMessageConverters method in fixed order execution.

If finer control is needed to clear other message converters or clear duplicate converters, it can be done by overriding extendMessageConverters. It is still possible that other WebMvcConfigurer instances can override this method, but it is very unlikely.

Spring Boot 1.x series

  1. Automatic configuration of Spring Boot, command-line-runner
  2. Learn about automatic configuration of Spring Boot
  3. Use of Spring Boot’s @propertysource annotation in integrating Redis

    * * *

    This issue focuses on topics such as backend technology, JVM troubleshooting and optimization, Java interview questions, personal growth and self-management, providing readers with front-line developer work and growth experience, looking forward to your harvest here.