菜鸟学习Spring——SpringMVC注解版解析不同格式的JSON串

111 阅读2分钟

> 原创地址:blog.csdn.net/gwblue/arti…> 一、概述> >         不同格式的JSON串传到后台来实现功能这个是我们经常要做的一件事,本篇博客就给大家介绍四种不同的JSON串传到后台后台如何用@RequestBody解析这些不同格式的JSON串的。> 二、代码展示> > 需要引用的jar包

1.xml配置> > > Web.xml

[html]  view plain  copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="java.sun.com/xml/ns/java…
  3.     xmlns:xsi="www.w3.org/2001/XMLSch…
  4.     xsi:schemaLocation="java.sun.com/xml/ns/java…
  5.     java.sun.com/xml/ns/java…
  6.     <servlet>  
  7.         <servlet-name>springMVC</servlet-name>  
  8.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  9.         <load-on-startup>1</load-on-startup>  
  10.     </servlet>  
  11.     <servlet-mapping>  
  12.         <servlet-name>springMVC</servlet-name>  
  13.         <url-pattern>*.spring</url-pattern>  
  14.     </servlet-mapping>  
  15.     <welcome-file-list>  
  16.         <welcome-file>index.jsp</welcome-file>  
  17.     </welcome-file-list>  
  18. </web-app>  

springMVC-servlet.xml

[html]  view plain  copy

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="www.springframework.org/schema/bean…

  3.     xmlns:mvc="www.springframework.org/schema/mvc"…

  4.     xmlns:p="www.springframework.org/schema/p" x…

  5.     xsi:schemaLocation="  

  6.         www.springframework.org/schema/bean…

  7.         www.springframework.org/schema/bean…

  8.         www.springframework.org/schema/cont…

  9.         www.springframework.org/schema/cont…

  10.           www.springframework.org/schema/mvc …

  11.         www.springframework.org/schema/mvc/…

  12.     <context:component-scan base-package="com.gaowei.JSON" />  

  13.     <mvc:annotation-driven />  

  14.       

  15.   

  16. </beans>  

2.java代码> > > Userinfo.java

[java]  view plain  copy

  1. package com.gaowei.entity;  
  2.   
  3. public class Userinfo {  
  4.     private String username;  
  5.     private String password;  
  6.     public String getUsername() {  
  7.         return username;  
  8.     }  
  9.     public void setUsername(String username) {  
  10.         this.username = username;  
  11.     }  
  12.     public String getPassword() {  
  13.         return password;  
  14.     }  
  15.     public void setPassword(String password) {  
  16.         this.password = password;  
  17.     }  
  18. }  

Test.java

[java]  view plain  copy

  1. package com.gaowei.JSON;  

  2.   

  3. import java.util.ArrayList;  

  4. import java.util.List;  

  5. import java.util.Map;  

  6.   

  7. import org.springframework.stereotype.Controller;  

  8. import org.springframework.web.bind.annotation.RequestBody;  

  9. import org.springframework.web.bind.annotation.RequestMapping;  

  10.   

  11.   

  12.   

  13.   

  14.   

  15. import com.gaowei.entity.Userinfo;  

  16.   

  17. @Controller  

  18. public class Test {  

  19.       

  20.     @RequestMapping(value="getJSON1")  

  21.     public void getJSON1(@RequestBody Userinfo userinfo){  

  22.         System.out.println("------getJSON1---start----");  

  23.         System.out.println(userinfo.getUsername());  

  24.         System.out.println(userinfo.getPassword());  

  25.         System.out.println("------getJSON1---end----");  

  26.     }  

  27.       

  28.     @RequestMapping(value="getJSON2")  

  29.     public void getJSON2(@RequestBody ArrayList<String> list){  

  30.         System.out.println("------getJSON2---start----");  

  31.         for (int i = 0; i < list.size(); i++) {  

  32.             System.out.println(list.get(i));  

  33.         }  

  34.         System.out.println("------getJSON2---end----");  

  35.     }  

  36.       

  37.     @RequestMapping(value="getJSON3")  

  38.     public void getJSON3(@RequestBody List<Map> list){  

  39.         System.out.println("------getJSON3---start----");  

  40.         for (int i = 0; i < list.size(); i++) {  

  41.             Map map=list.get(i);  

  42.             System.out.println(map.get("username")+" "+map.get("password"));  

  43.         }  

  44.         System.out.println("------getJSON3---end----");  

  45.     }  

  46.       

  47.     @RequestMapping(value="getJSON4")  

  48.     public void getJSON4(@RequestBody Map map){  

  49.         System.out.println("------getJSON4---start----");  

  50.         System.out.println(map.get("username"));  

  51.         List<Map> workList=(List)map.get("work");  

  52.         for (int i = 0; i < workList.size(); i++) {  

  53.             Map eachAddressMap=workList.get(i);  

  54.             System.out.println("address="+eachAddressMap.get("address"));  

  55.         }  

  56.         Map schoolMap=(Map)map.get("school");  

  57.         System.out.println(schoolMap.get("name"));  

  58.         System.out.println(schoolMap.get("address"));  

  59.         System.out.println("------getJSON4---end----");  

  60.     }  

  61. }  

3.界面代码> > > Test.jsp

[html]  view plain  copy

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>  

  2.  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

  3. <html>  

  4.     <head>  

  5.         <script src="jquery-1.3.2.js">  

  6.         </script>  

  7.         <script src="json2.js">  

  8.         </script>  

  9.         <script>  

  10.             function userinfo(username, password){  

  11.                 this.username = username;  

  12.                 this.password = password;  

  13.             }  

  14.               

  15.             function sendAjax1(){  

  16.                 var userinfoRef = new userinfo('中国', '中国人');  

  17.                 var jsonStringRef = JSON.stringify(userinfoRef);  

  18.                 $.ajax({  

  19.                     type: "POST",  

  20.                     data: jsonStringRef,  

  21.                     url: "getJSON1.spring?t=" + new Date().getTime(),  

  22.                     contentType: "application/json"  

  23.                 });  

  24.             }  

  25.   

  26.             function sendAjax2(){  

  27.                 var myArray =new Array();  

  28.                 myArray[0]="中国1";  

  29.                 myArray[1]="中国2";  

  30.                 myArray[2]="中国3";  

  31.                 myArray[3]="中国4";  

  32.                 var jsonString=JSON.stringify(myArray);  

  33.                   $.ajax({  

  34.                         type: "POST",  

  35.                         data: jsonString,  

  36.                         url: "getJSON2.spring?t=" + new Date().getTime(),  

  37.                         contentType: "application/json"  

  38.                     });  

  39.                 }  

  40.   

  41.   

  42.             function sendAjax3(){  

  43.                     var myArray=new Array();  

  44.                     myArray[0]= new userinfo('中国1', '中国人1');  

  45.                     myArray[1]= new userinfo('中国2', '中国人2');  

  46.                     myArray[2]= new userinfo('中国3', '中国人3');  

  47.                     myArray[3]= new userinfo('中国4', '中国人4');  

  48.                     var jsonString=JSON.stringify(myArray);  

  49.                       $.ajax({  

  50.                             type: "POST",  

  51.                             data: jsonString,  

  52.                             url: "getJSON3.spring?t=" + new Date().getTime(),  

  53.                             contentType: "application/json"  

  54.                         });  

  55.                 }  

  56.   

  57.             function sendAjax4(){  

  58.                 var jsonObject={  

  59.                     "username":"accp",  

  60.                     "work":[{  

  61.                         "address":"address1"  

  62.                         },{  

  63.                         "address":"address2"      

  64.                             }],  

  65.                         "school":{  

  66.                             "name":"tc",  

  67.                             "address":"pjy"  

  68.                             }  

  69.                         }  

  70.                 var jsonString=JSON.stringify(jsonObject);  

  71.                   $.ajax({  

  72.                         type: "POST",  

  73.                         data: jsonString,  

  74.                         url: "getJSON4.spring?t=" + new Date().getTime(),  

  75.                         contentType: "application/json"  

  76.                     });  

  77.             }  

  78.         </script>  

  79.     </head>  

  80.     <body>  

  81.         <input type="button" onclick="sendAjax1()" value="sendAjax1"/>  

  82.         <br/>  

  83.         <input type="button" onclick="sendAjax2()" value="sendAjax2">  

  84.         <br/>  

  85.         <input type="button" onclick="sendAjax3()" value="sendAjax3">  

  86.         <br/>  

  87.         <input type="button" onclick="sendAjax4()" value="sendAjax4">  

  88.         <br/>  

  89.     </body>  

  90. </html>  

4.效果图

> 三、总结。> >         这里要注意jar包的引用不要把Spring的所有jar包都引用了会引起jar包冲突而导致报HTTP 415的错误。@RequestBody这个方法很强大可以把JSON串转化为实体类、ArryList、Map等对象。这样的方法让我们开发人员开发效率大大的提高了