Convert the request message into a Java object
Convert the Java object into a response message
getSupportedMediaTypes: Get the supported MediaType collection (such as: text/html, text/plain, application/json)
canRead: Determine whether it can be read (request)
read: Convert the format of the request data (called when the return value of the canRead method is true )
canWrite: Determine whether it can be written (response)
write: Convert the format of the response data (when the return value of the canWrite method is true Call)
SpringMVC will automatically configure some HttpMessageConverter (addDefaultHttpMessageConverters of the WebMvcConfigurationSupport class) method when it starts up
The source code is as follows:
protected final void addDefaultHttpMessageConverters(List> messageConverters) { messageConverters.add(new ByteArrayHttpMessageConverter()); messageConverters.add(new StringHttpMessageConverter()); messageConverters.add(new ResourceHttpMessageConverter()); messageConverters.add(new ResourceRegionHttpMessageConverter()); try { messageConverters.add(new SourceHttpMessageConverter<>()); } catch (Throwable ex) { // Ignore when no TransformerFactory implementation is available... } messageConverters.add(new AllEncompassingFormHttpMessageConverter()); if (romePresent) { messageConverters.add(new AtomFeedHttpMessageConverter()); messageConverters.add(new RssChannelHttpMessageConverter()); } if (jackson2XmlPresent) { Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml(); if (this.applicationContext != null) { builder.applicationContext(this.applicationContext); } messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build())); } else if (jaxb2Present) { messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2Present) { Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json(); if (this.applicationContext != null) { builder.applicationContext(this.applicationContext); } messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build())); } else if (gsonPresent) { messageConverters.add(new GsonHttpMessageConverter()); } else if (jsonbPresent) { messageConverters.add(new JsonbHttpMessageConverter()); } if (jackson2SmilePresent) { Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile(); if (this.applicationContext != null) { builder.applicationContext(this.applicationContext); } messageConverters.add(new MappingJackson2SmileHttpMessageConverter(builder.build())); } if (jackson2CborPresent) { Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.cbor(); if (this.applicationContext != null) { builder.applicationContext(this.applicationContext); } messageConverters.add(new MappingJackson2CborHttpMessageConverter(builder.build())); } }
Partial message converter parsing
Name | Description |
---|---|
MappingJackson2HttpMessageConverter | Responsible for reading and writing JSON format data (using Jackson) |
AllEncompassingFormHttpMessageConverter | Responsible for reading and writing Form form data |
Jaxb2RootElementHttpMessageConverter | Responsible for reading and writing XML format data (using JAXB) |
ByteArrayHttpMessageConverter | Responsible for reading and writing Binary format data |
StringHttpMessageConverter | is responsible for reading and writing string format data |
ResourceHttpMessageConverter | is responsible for Read and write resource file data |
SourceHttpMessageConverter | Responsible for reading and writing resource data |
The system has a set of message converters configured by default.
The processing process will match the appropriate message converter in the order of the collection. If there is a suitable one, the message converter will be used for processing (reading, writing), and subsequent message converters will not be executed.
For a custom message converter to take effect, it must be placed in front of a message converter of the same type in the collection. For the reason, please refer to the second point.
Thinking: Since the custom message converter must be placed in front of the same type of message converter in the collection, can the original message converter in the collection be directly modified to achieve the custom effect without having to Add one (not researched yet).
When adding a custom message converter, pay attention to whether the default message converter is in effect
The WebMvcConfigurer.configureMessageConverters method will override the default message converter set
WebMvcConfigurer.extendMessageConverters method does not override the default message converter collection
The above is the detailed content of How to use SpringBoot HttpMessageConverter message converter. For more information, please follow other related articles on the PHP Chinese website!