Recently, many interactions have to deal with the native
HttpServletResponse. Read body data from
HttpServletRequest and encapsulate it into some kind of data structure; write data to
HttpServletResponse and respond. The traditional way of writing is very inelegant, so today we introduce you to a more elegant way.
HttpMessageConverter is a message converter model provided by the Spring Framework, a policy interface for converting between HTTP requests and responses. It can read the input message
HttpInputMessage; it can also write the output message
Spring MVC message conversion is done through the implementation of this interface. There are many implementations of
Typically Spring MVC handles Form form submissions, JSON, XML, strings, and even Protobuf by the
HttpMessageConverter implementation, where the body parameters passed from the front end to the back end and the data returned from the back end to the front end are all converted by This interface does the conversion. In Spring IoC (Spring MVC environment) there is also a container
HttpMessageConverter that holds the
We can just use it. So how exactly do we use it? Well, first we need to figure out what
HttpOutputMessage are for.
HttpInputMessage represents an HTTP input message, consisting of a request header headers and a readable request body body, usually implemented by a server-side HTTP request handle or a client-side HTTP response handle.
HttpServletRequest is the extension interface of
ServletRequest, which provides the request information of HTTP Servlet and also contains the request header and request body, so the two are related. We just need to find out the actual relationship between the two to allow
HttpMessageConverter to read and process the request information carried by
And to be honest, we did find it.
ServletServerHttpRequest is not only an implementation of
HttpInputMessage, it also holds an
HttpServletRequest instance property, and all operations of
ServletServerHttpRequest are based on
HttpServletRequest. We can inject
HttpServletRequest instances into it via constructs so that
HttpMessageConverter can handle
Extracting the request body
The scenario in focus here is the use of
HttpMessageConverter in a Servlet filter, which is less recommended in Spring MVC to manipulate
HttpServletRequest. I chose
FormHttpMessageConverter, which is usually used to handle
application/x-www-form-urlencoded requests. We write a filter to intercept the request extract body .
Then execute a request of type
The console will print the following message.
ServletServerHttpRequest and there is
ServletServerHttpResponse, the general principle is similar. It happens to be the opposite of
ServletServerHttpRequest, so if we need to deal with the response, for example, if we want to write a JSON response via
HttpServletResponse, we can probably write it like this.
HttpMessageConverter abstracts the HTTP message conversion strategy and can help us handle some request response issues gracefully. One thing to note though is that the request body body can only be read once, even if it is wrapped in
ServletServerHttpRequest, so be aware of the difference with