In this paper, a simple example of a small, brief SpringMVC development, Json related applications, only to learn to share, if any inadequacies, please correct me.
What is Json?
- JSON refers to the JavaScript Object Notation (JavaScript Object Notation), a lightweight data interchange format text, smaller than XML, faster, and easier to resolve.
- JSON is independent of the language: JSON with Javascript syntax to describe data objects, but JSON is still independent of language and platform.
- JSON self descriptive, easier to understand.
SpringMVC support Json steps
On the basis of above SpringMVC annotation support, the need to support returns Json data format, the following steps:
1. Import jackson three support packages (if not import the package, will be reported 406 errors)
Jar package need to import as follows:
1 // three needs requires human jackson packet 2 jackson-2.2.1-Annotations .jar . 3 jackson-Core-2.2.1 .jar . 4 jackson-DataBind-2.2.1.jar
2. Add a note on the method @ResponseBody
As follows:
1 / ** 2 * If you want to return Json, notes the need to increase @ResponseBody, otherwise it will return to the path as 3 * 406 if an error is returned, the package is missing 4 * @return 5 * / 6 @ RequestMapping ( "/ json01" ) 7 @ResponseBody . 8 public String json01 () { . 9 System.out.println ( "JSON is called ....." ); 10 return "the Hello World !!!" ; . 11 }
3. Access
Open your browser and enter the URL as follows:
SpringMVC different types of return Json
1. Return array
As shown below, directly back String [] to.
1 / * 2 * Returns an array . 3 * @return . 4 * / . 5 @RequestMapping ( "/ json02" ) . 6 @ResponseBody . 7 public String [] json02 () { . 8 System.out.println ( "... is called JSON .. " ); . 9 String [] = ARR new new String [] {" Bob "," flower " }; 10 return ARR; . 11 }
Page is shown below:
2. Return the class object
As follows: User object returned directly
1 / * 2 * returns the object . 3 * @return . 4 * / . 5 @RequestMapping ( "/ json03" ) . 6 @ResponseBody . 7 public the User json03 () { . 8 System.out.println ( "JSON is called ..... " ); . 9 the User User = new new the User (1L," Joe Smith ",. 3, new new a Date ()); 10 return User; . 11 }
As shown below: returns the format objects Json
If the object is returned in the date format, you will need to deal with the format returned
1 / * 2 * from the background to the foreground, formatting . 3 * @return . 4 * / . 5 @JsonFormat (pattern = "the MM-dd-YYYY HH: mm: SS", TimeZone = "GMT +. 8" ) . 6 public getDate DATE () { . 7 return DATE; . 8 } . 9 10 / ** . 11 * from front to back, the received setting parameters format 12 is * @param DATE 13 is * / 14 @DateTimeFormat (pattern = "the MM-dd-YYYY" ) 15 public void setDate (a Date DATE) { 16 the this .date = date; 17 }
3. Return to the list of objects
As follows: returns List <User> object list
1 / * 2 * returns the object list . 3 * @return . 4 * / . 5 @RequestMapping ( "/ json04" ) . 6 @ResponseBody . 7 public List <the User> json04 () { . 8 System.out.println ( "JSON is called. .... " ); . 9 List <the User> LST = new new the ArrayList <the User> (); 10 the User ZS = new new the User (1L," Joe Smith ",. 3, new new a Date ()); . 11 the User LS = new new the User (2l, "John Doe",. 4, new new a Date ()); 12 is User ww=new User(3l,"王五",5,new Date()); 13 lst.add(zs); 14 lst.add(ls); 15 lst.add(ww); 16 return lst; 17 }
As shown below: it returns an array of object
Remark
Mo listen Chuan Lin beat Ye, why Yin Xiao and Xing Xu. Zhu Zhang Mans shoes light horse wins, Who's Afraid? Yisuoyanyu Renping Sheng.