SPRINGMVC processing JSON------Httpmessageconverter interface

Source: Internet
Author: User

One, SPRINGMVC processing the use of JSON 1, add dependent jar package
<Dependency>    <groupId>Com.fasterxml.jackson.core</groupId>    <Artifactid>Jackson-annotations</Artifactid>    <version>2.8.7</version></Dependency><Dependency>    <groupId>Com.fasterxml.jackson.core</groupId>    <Artifactid>Jackson-core</Artifactid>    <version>2.8.7</version></Dependency><Dependency>    <groupId>Com.fasterxml.jackson.core</groupId>    <Artifactid>Jackson-databind</Artifactid>    <version>2.8.7</version></Dependency>
2.Write the method of the target , so that it returns JSON corresponding to the image or set ;
3. Add @ResponseBody Annotations on the method:
@Controller @requestmapping ("/USER") Public classusercontroller{@AutowiredPrivateUserService UserService; @RequestMapping ("/getuserlist") @ResponseBody  PublicMap<string, object>getuserlist (model model) {List<User> userlist =userservice.getuserlist (); Map<string, object> map =NewHashmap<>(); Map.put ("Aadata", userlist); returnmap; }}

Second, Httpmessageconverter1. Httpmessageconverter Information

httpmessageconverter<t> is a new addition to Spring3.0 port, responsible for please request information convert to a type t type T) output as response information

    

2, httpmessageconverter operation principle

(1)Httpinputmessage The requested information into the InputStream object First, InputStream again by Httpmessageconverter Convert to Java object required by SPRINGMVC;

(2) SPRINGMVC Returns a Java object and httpmessageconverter to the response information, and then Httpoutputmessage Converts the response information to OutputStream , and then give a response.

3, Httpmessageconveter of the implementation of the class

4. SPRINGMVC Httpmessageconverter implementation class loaded by default

(1) Dispatcherservlet 6 implementation classes that load Httpmessageconveter by default, as follows:

(2) After adding the Jackson jar package, load the implementation class of 7 Httpmessageconverter when it starts up, as follows:

5. Using Httpmessageconverter

(1) Examples of @ResponseBody and @RequestBody:

(2) Examples of httpentity and responseentity

SPRINGMVC processing JSON------Httpmessageconverter interface

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.