Spring Boot custom HTTP message converter, springboot

Source: Internet
Author: User

Spring Boot custom HTTP message converter, springboot

When building a RESTful data service, we defined controller and repositories and modified them with some annotations, but so far, we have not performed object conversion-converting java object to HTTP data output stream. At the underlying layer of Spring Boot, HttpMessageConverters relies on the Jackson library to output Java entity classes in JSON format. When multiple converters are available, select the most suitable one based on the message object type and the required content type.

In the text of the SpringMVC source code analysis message converter HttpMessageConverter, a figure clearly shows the location of the message converter.

Location of the message Converter

The objective of the message converter is to convert the HTTP input request format to the Java object, and convert the Java object to the HTTP output request. Some message converters only support multiple data types, some support only multiple output formats, and some support both. For example, MappingJackson2HttpMessageConverter can convert a Java object to application/json, while ProtobufHttpMessageConverter only supports com. google. protobuf. message Type input, but can output application/json, application/xml, text/plain and application/x-protobuf in so many formats.

How Do

There are three ways to configure the message converter in the project, the main difference is the customizable and easy-to-use measurement.

Add @ Bean definition to the WebConfiguration class

@Beanpublic ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {  return new ByteArrayHttpMessageConverter();}

Override configureMessageConverters method to extend the linked list of the existing message converter;

@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {  converters.add(new ByteArrayHttpMessageConverter());}

For more control, you can override 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());}

Analysis

Spring provides multiple methods to accomplish the same task. It depends on whether we are more convenient or customizable.

What are the differences between the three methods mentioned above?

Defining HttpMessageConverter using @ Bean is the easiest way to add a message converter to a project, similar to adding Servlet Filters. If Spring scans the HttpMessageConverter type bean, it is automatically added to the call chain. We recommend that you inherit WebConfiguration from webmvcjavaseradapter in the project.

It is convenient to add a custom Converter by rewriting the configureMessageConverters method, but there is a weakness: if there are multiple webmvccyclers instances in the project (defined by ourselves or provided by Spring Boot by default ), the overwritten configureMessageConverters method cannot be executed in a fixed order.

If you need more refined control: to clear other message converters or clear repeated converters, you can rewrite extendMessageConverters. The possibility is that other WebMvcConfigurer instances can also rewrite this method, however, this probability is very small.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.