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