In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the SpringMVC message converter HttpMessageConverter, the article is very detailed, has a certain reference value, interested friends must read it!
Abstract
SpringMVC uses message converter to realize automatic conversion between request message and object, object and response message.
In SpringMVC, @ RequestBody and @ ResponseBody annotations can be used to complete the conversion of request message to object and object to response message respectively. The underlying flexible message conversion mechanism is the newly introduced HttpMessageConverter message converter mechanism in Spring3.x.
# the abstraction of the Http request comes back to the request-response, that is, parsing the request body and then returning the response message, which is the most basic Http request process. We know that in the servlet standard, you can use the following methods in the javax.servlet.ServletRequest interface:
Public ServletInputStream getInputStream () throws IOException
To get a ServletInputStream. In this ServletInputStream, you can read all the contents of an original request message. Similarly, in the javax.servlet.ServletResponse interface, you can use the following methods:
Public ServletOutputStream getOutputStream () throws IOException
To get a ServletOutputStream, this ServletOutputSteam, inherited from OutputStream in java, allows you to output the contents of the Http response message.
Let's try to think like the designers of SpringMVC. We know that Http request and response messages are essentially strings. When the request message comes to the java world, it will be encapsulated into an ServletInputStream input stream for us to read. The response message outputs the response message through an output stream of ServletOutputStream.
We can only read the original string message from the stream, and similarly, we can only write the original characters to the output stream. In the java world, the processing of business logic is based on business meaningful objects, so when the message arrives at SpringMVC and goes out from SpringMVC, there is an impedance problem from string to java object. This process cannot be manually converted by the developer. We know that in Struts2, OGNL is used to deal with this problem, while in SpringMVC, it is the HttpMessageConverter mechanism. Let's first look at two interfaces.
The class # HttpInputMessage is the abstraction of a Http request message within SpringMVC. In the read () method of HttpMessageConverter, there is a formal parameter of HttpInputMessage, which is the internal abstraction of the recipient "request message" acted by the message converter of SpringMVC. The message converter extracts the message from the "request message" according to the rules and converts it into the object declared in the method parameter.
Package org.springframework.http;import java.io.IOException;import java.io.InputStream;public interface HttpInputMessage extends HttpMessage {InputStream getBody () throws IOException;}
The class # HttpOutputMessage is the abstraction of a Http response message within SpringMVC. In the write () method of HttpMessageConverter, there is a formal parameter of HttpOutputMessage, which is the internal abstraction of the receptor "response message" acted by the message converter of SpringMVC. The message converter writes the "response message" into the response message according to certain rules.
Package org.springframework.http;import java.io.IOException;import java.io.OutputStream;public interface HttpOutputMessage extends HttpMessage {OutputStream getBody () throws IOException;}
# HttpMessageConverter abstracts the highest-level interface of a message converter and describes the general characteristics of a message converter. We can understand how Spring3.x designers think about this mechanism from the methods defined in this interface.
Package org.springframework.http.converter;import java.io.IOException;import java.util.List;import org.springframework.http.HttpInputMessage;import org.springframework.http.HttpOutputMessage;import org.springframework.http.MediaType;public interface HttpMessageConverter {boolean canRead (Class clazz, MediaType mediaType); boolean canWrite (Class clazz, MediaType mediaType); List getSupportedMediaTypes (); T read (Class)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.