ArticleDirectory
- Httpmessageconverter Interface
- How does spring find the best httpmessageconverter?
- Example:
@ Responsebody & @ requestbody
Role?
@ Requestbody: insert the HTTP Request body into the method, and use the appropriate httpmessageconverter to write the Request body to an object.
@ Responsebody: the content or object is returned as the HTTP response body. Using @ responsebody, the view processing part is skipped. Instead, it calls httpmessageconverter, which is suitable for writing the returned value to the output stream.
Httpmessageconverter Interface
<MVC: annotation-driven/> after it is enabled, it initializes seven Converters for annotationmethodhandleradapter. You can call the getmessageconverts () method of the annotationmethodhandleradapter class to obtain a list of converters.
The annotationmethodhandleradapter is initialized by default. (You can also add custom converter)
Bytearrayhttpmessageconverter
Stringhttpmessageconverter
Resourcehttpmessageconverter
Sourcehttpmessageconverter <t>
Xmlawareformhttpmessageconverter
Jaxb2rootelementhttpmessageconverter
Mappingjacksonhttpmessageconverter
How does spring find the best httpmessageconverter?
1. First, obtain all the registered httpmessageconverter sets.
2. Search for the types that the client can receive in the request header of the client,
For example, accept application/JSON and application/XML form a set.
3. The return values of all httpmessageconverter Canread and canwrite methods are Boolean. Check whether the httpmessageconverter supports reading and writing of the current request. Read the @ requestbody annotation and write the @ responsebody annotation.
4. traverse the httpmessageconverter set and match the previously obtained acceptable type. If the matching is successful, use the first matching httpmessageconverter and return (usually match the returned object type through accept)
For example
Stringhttpmessageconverter
All types of string and accept are supported.
Mappingjacksonhttpmessageconverter
Supports map list object and so on. Accept: Application/JSON
Example:
Objectives:
The responsebody is used to present the JSON and XML results of an object for different request addresses based on the head's accept.
Because <context: annotation-config/>
Annotationmethodhanlderadapter will be initialized by default, but we need to modify the handleradapter in the XML content returned, so the configuration file is as follows:
<Context: component-scan base-package ="Com. Controls"/>
<Context: annotation-config/>
<Bean class ="Org. springframework. Web. servlet. MVC. annotation. annotationmethodhandleradapter">
<Property name ="Messageconverters">
<List>
<Ref bean ="Stringhttpmessageconverter"/>
<Ref bean ="Jsonhttpmessageconverter"/>
<Ref bean ="Marshallinghttpmessageconverter"/>
</List>
</Property>
</Bean>
<Bean id ="Stringhttpmessageconverter"Class ="Org. springframework. http. converter. stringhttpmessageconverter"/>
<Bean id ="Jsonhttpmessageconverter"Class ="Org. springframework. http. converter. JSON. mappingjacksonhttpmessageconverter"/>
<Bean id ="Marshallinghttpmessageconverter"Class ="Org. springframework. http. converter. xml. marshallinghttpmessageconverter">
<Constructor-Arg ref ="Jaxbmarshaller"/>
<Property name ="Supportedmediatypes"Value ="Application/XML"> </Property>
</Bean>
<Bean id ="Jaxbmarshaller"Class ="Org. springframework. oxm. jaxb. jaxb2marshaller">
<Property name ="Classestobebound">
<List>
<Value> com. model. User </value>
</List>
</Property>
</Bean>
Note: To useJaxb2marshallerWe need to specify the corresponding entities, such as the user class.
@ Xmlrootelement annotation, which needs to be introduced
ImportJavax. xml. Bind. annotation. xmlrootelement;
This package.
Request Handling Methods in Controller
@ Requestmapping (value = "/user/{userid}", method = requestmethod.Get)
Public@ Responsebody user queryuser (@ pathvariable ("userid ")LongUserid ){
Calendar d = calendar.Getinstance();
D. Set (1987, 12, 9 );
User u =NewUser ();
U. setuserid (userid );
U. setusername ("Zhaoyang ");
U. setbirth (D. gettime ());
ReturnU;
}
Then we use the curl tool for testing.
For example: