Recently, many interactions have to deal with the native HttpServletRequest
and 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
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 HttpOutputMessage
.
Spring MVC message conversion is done through the implementation of this interface. There are many implementations of HttpMessageConverter
.
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 HttpMessageConverters
.
We can just use it. So how exactly do we use it? Well, first we need to figure out what HttpInputMessage
and HttpOutputMessage
are for.
HttpInputMessage
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.
And 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 HttpServletRequest
.
ServletServerHttpRequest
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 HttpServletRequest
indirectly.
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 POST
with Content-Type
of application/x-www-form-urlencoded
.
The console will print the following message.
|
|
ServletServerHttpResponse
There is 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.
|
|
Summary
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 HttpServletRequestWrapper
.
Reference
https://my.oschina.net/10000000000/blog/5389683