SPRINGMVC Source Analysis--httpmessageconverter Data conversion (a)

Source: Internet
Author: User
Tags readable

Previous blog We have introduced a lot of SPRINGMVC related modules, next we introduce the SPRINGMVC to get the parameters and return the result value of processing. Although in the previous blog The old field has introduced the parameter processor and the return value processor separately:

(1) SPRINGMVC parameter value processor SPRINGMVC Source Analysis--handlermethodargumentresolver parameter parser (i)

(2) Springmvc return value processor SPRINGMVC Source Analysis--handlermethodreturnvaluehandler return value parser (i)

But for the parameters and return value of the real processing class and have a detailed introduction, then the Old field with a few blog to introduce the data message on the parameters and return value of the processing process.

SPRINGMVC provides an interface httpmessageconverter for processing operations on data message to handle conversion of parameter values and return values.

Httpmessageconverter provides a simple way to determine whether a readable, writable, or read-write operation is supported.

Public interface Httpmessageconverter<t> {//Determines whether the data type is readable Boolean canRead (class<?> Clazz, mediatype mediatype );//Determine if the data is writable by Boolean canWrite (Class<?> Clazz, mediatype mediatype);//Get Supported data types list<mediatype> Getsupportedmediatypes ();//Read the parameter value, convert it to the desired type T read (class<? extends T> Clazz, Httpinputmessage inputmessage) Throws IOException, httpmessagenotreadableexception;//sends the return value to the requestor void write (T T, mediatype ContentType, Httpoutputmessage outputmessage) throws IOException, Httpmessagenotwritableexception;}

Of course, in the aspect of read-write operation Springmvc also provides read operation interface Httpinputmessage and write operation interface Httpoutputmessage to complete the reading and writing operation of the data.

The interface provided by Httpinputmessage is to convert the data in the body into an input stream

Public interface Httpinputmessage extends Httpmessage {/** * Return the body of the message as an input stream. * @return The input stream body (never {@code null}) * @throws IOException in case of I/O Errors *///get data in body as input stream InputStream GETB Ody () throws IOException;}
The interface provided by the Httpoutputmessage is to convert the data in the body to the output stream

Public interface Httpoutputmessage extends Httpmessage {/** * Return the body of the message as an output stream. * @retur n the output stream body (never {@code null}) * @throws IOException in case of I/O Errors *///convert data to output stream OutputStream GetBody () throws IOException;}

The parent interface Httpmessage provides a way to read the information in the header

Public interface Httpmessage {/** * Return to the headers of this message. * @return A corresponding Httpheaders object (neve R {@code null}) *///gets the information in the header httpheaders getheaders ();}

Httpmessageconverter interface and Implementation class


Before Springmvc enters the ReadString method, the appropriate Httpmessageconverter implementation class is selected according to the @requestbody annotation to parse the request parameters into the string variable. Specifically, the Stringhttpmessageconverter class is used, and its CanRead () method returns True, and its read () method reads the request parameter from the request, bound to the string variable of the ReadString () method.
When Springmvc executes the ReadString method, the return value identifies the @responsebody,springmvc will use the Write () method of Stringhttpmessageconverter. Writes the result as a string value to the response message, of course, at which point the CanWrite () method returns True.

We can use the following diagram to briefly describe the process.





SPRINGMVC Source Analysis--httpmessageconverter Data conversion (a)

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.