Spring Boot project, how to customize HTTP message converter

In the process of building RESTful data service, we define the controller, repositories, and modifying them with some of the notes, but until now we have not performed a conversion target - to convert a java entity object to the HTTP data stream output.

Spring Boot bottom by HttpMessageConverters output Java entity class library and Jackson JSON format. When there are a plurality of converters are available, the message content type and the object type select the most appropriate according to the use of the converter.

In SpringMVC source analysis (E) - message converter HttpMessageConverter article, there is a map showing the location can clearly get the message converter.

Transducer location message

Target message converter is:

  1. Incoming request conversion HTTP format, to the Java object;
  2. Conversion output Java object to HTTP requests.

Some message converter only supports a plurality of types of data, some only support a plurality of output formats, as well as a combination of both.

For example: MappingJackson2HttpMessageConverter can convert Java objects to application / json, and ProtobufHttpMessageConverter only supports com.google.protobuf.Message type of input, but can output application / json, application / xml, text / plain and application / x-protobuf so much format.

practice

There are three ways to configure the message converter, mainly from the project customization and ease of use to measure two aspects.

  1. Add @Bean class definition WebConfiguration
@Bean
public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
    return new ByteArrayHttpMessageConverter();
}
  1. Rewritten (the override) configureMessageConverters method, the message converter extend existing list;
@Override
public 
void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new ByteArrayHttpMessageConverter());
}
  1. More control can override extendMessageConverters method, emptied first converter list, then add a custom converter.
@Override
public 
void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.clear();
    converters.add(new ByteArrayHttpMessageConverter());
}

analysis

Spring Boot provides several ways to accomplish the same task, depending on the choice of which we are more focused on convenience is more focused on customizability.

The above-mentioned three methods have different?

By @Bean defined HttpMessageConverter is added easiest way to project the message converter, add the Servlet Filters This is similar to the previously mentioned. If Spring scanned HttpMessageConverter type of bean, it will be automatically added to the call chain. Recommended allow the project WebConfiguration inherited from WebMvcConfigurerAdapter.

By rewriting configureMessageConverters method of adding a custom converter is very convenient, but there is one weakness: If multiple instances WebMvcConfigurers exists in the project (our own definition, or Spring Boot provided by default), the method can not be rewritten to ensure configureMessageConverters performed in a fixed order.

If you need finer control: Clear the message converter or other clear repetition of the converter, you can rewrite extendMessageConverters completed, there is still such a possibility: WebMvcConfigurer other instances can override this method, but the probability is very small.

Spring Boot 1.x series

  1. Spring Boot automatic configuration, Command-line-Runner
  2. Understanding Automatic configuration of Spring Boot
  3. Spring Boot of @PropertySource notes in the integration of Redis

This focus on the number of back-end technology, JVM troubleshooting and optimization, Java interview questions, personal growth and self-management, and other topics, providing front-line developers work and growth experience for the reader, you can expect to gain something here.
Jwadu

Guess you like

Origin www.cnblogs.com/javaadu/p/11735745.html