In the process of building RESTful data services, we defined controllers and repositories and modified them with some annotations, but so far we have not performed object conversion - converting java entity objects into HTTP data output streams. Spring Boot's underlying layer uses HttpMessageConverters to output Java entity classes into JSON format by relying on the Jackson library. When multiple converters are available, select the most suitable converter to use according to the message object type and the required content type.
In the article HttpMessageConverter, SpringMVC source code analysis, there is a diagram that clearly indicates the location of the message converter.
Location of message converter
The goal of the message converter is: the conversion of HTTP input request format to Java objects; the conversion of Java objects to HTTP output requests. Some message converters only support multiple data types, some support multiple output formats, and some provide both. For example: MappingJackson2HttpMessageConverter can convert Java objects to application/json, while ProtobufHttpMessageConverter only supports inputs of com.google.protobuf.Message, but can output application/json, application/xml, text/plain and application/x-protobuf.
How Do
There are three ways to configure the message converter in the project, the main difference is the measurement of customizability and ease of use.
Add @Bean definition to WebConfiguration class
@Beanpublic ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { return new ByteArrayHttpMessageConverter();} Override the configureMessageConverters method to extend the existing message converter linked list;
@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new ByteArrayHttpMessageConverter());} For more control, you can rewrite the extendMessageConverters method, first clear the converter list, and then add a custom converter.
@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.clear(); converters.add(new ByteArrayHttpMessageConverter());}analyze
Spring provides multiple ways to accomplish the same task, and which one is chosen depends on whether we focus more on convenience or customizability.
What are the differences between the three methods mentioned above?
Defining HttpMessageConverter via @Bean is the easiest way to add a message converter to your project, similar to the aforementioned addition of Servlet Filters. If Spring scans to a bean of type HttpMessageConverter, it will be automatically added to the call chain. It is recommended to inherit the WebConfiguration in the project from WebMvcConfigurerAdapter.
It is convenient to add a custom converter by rewriting the configureMessageConverters method, but there is a weakness: if there are multiple instances of WebMvcConfigurers in the project (which we define ourselves or provided by Spring Boot by default), it is not guaranteed that the rewrite configureMessageConverters method is executed in a fixed order.
If more granular control is needed: clearing other message converters or clearly duplicate converters, it can be done by rewriting extendMessageConverters, and there is still this possibility: other WebMvcConfigurer instances can also rewrite this method, but the chance is very small.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.