Guide Package:
In addition to some spring packages, we also need Jackson-annotations.jar, Jackson-core.jar, Jackson-databind.jar these three packages
To open @responsebody annotations:
@responsebody annotations are opened in spring-mvc.xml via <mvc:annotation-driven/>
Labeling business methods with @responsebody
PackageCom.loger.controller;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportCom.loger.entity.User;/*** Responsebody Annotations, the following types of data can be made JSON: * 1) basic data type, such as Boolean, String, int, etc. * 2) MAP type Data * 3) Collection or array * 4) Entity Object * 5) Entity object Collection **/@Controller @requestmapping ("/test") Public classJsoncontroller {@RequestMapping ("Test1.do") @ResponseBody Public Booleantest1 () {return true; } @RequestMapping ("/test2.do") @ResponseBody PublicMap<string, object>test2 () {Map<string, object> map =NewHashmap<string,object>(); Map.put ("id", "s20070"); Map.put ("Name", "Cheng"); returnmap; } @RequestMapping ("/test3.do") @ResponseBody PublicList<string>test3 () {List<String> list =NewArraylist<>(); List.add ("AAA"); List.add ("BBB"); List.add ("CCC"); returnlist; } @RequestMapping ("/test4.do") @ResponseBody PublicUser test4 () {User User=NewUser (); User.setid ("s20068"); User.setname ("Yu Qingpo"); User.setage (21st); returnuser; } @RequestMapping ("/test5.do") @ResponseBody PublicList<user>Test5 () {List<User> list =NewArraylist<>(); User User1=NewUser (); User1.setid ("s200681"); User1.setname ("Yu Qingpo 1"); User1.setage (21st); List.add (user1); User User2=NewUser (); User2.setid ("S200682"); User2.setname ("Yu Qingpo 2"); User2.setage (21st); List.add (User2); User User3=NewUser (); User3.setid ("s200683"); User3.setname ("Yu Qingpo 3"); User3.setage (21st); List.add (USER3); returnlist; }}
Output Result:
Springmvc using the @responsebody annotation callout business method, the return value of the business method is made into JSON output to the page