json資料交換

來源:互聯網
上載者:User

標籤:ax1   方法   nis   rip   qpi   bst   png   sch   cs6   

1.1   為什麼要進行json資料互動

json資料格式在介面調用中、html頁面中較常用,json格式比較簡單,解析還比較方便。

比如:webservice介面,傳輸json資料.

 

1.2   springmvc進行json互動

1、請求json、輸出json,要求請求的是json串,所以在前端頁面中需要將請求的內容轉成json,不太方便。

 

2、請求key/value、輸出json。此方法比較常用。

 

1.3    環境準備

 

1.3.1   載入json轉的jar包

springmvc中使用jackson的包進行json轉換(@requestBody和@responseBody使用下邊的包進行json轉),如下:

 

 

1.3.2     配置json轉換器

在註解適配器中加入messageConverters

1 <!--註解適配器 -->2     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">3         <property name="messageConverters">4         <list>5         <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>6         </list>7         </property>8     </bean>

 

注意:如果使用<mvc:annotation-driven /> 則不用定義上邊的內容。

 

 

1.4   json互動測試

 

1.4.1    輸入json串,輸出是json串1.4.1.1     jsp頁面

使用jquery的ajax提交json串,對輸出的json結果進行解析。

 1 //請求json,輸出是json 2 function requestJson(){ 3      4     $.ajax({ 5         type:‘post‘, 6         url:‘${pageContext.request.contextPath }/requestJson.action‘, 7         contentType:‘application/json;charset=utf-8‘, 8         //資料格式是json串,商品資訊 9         data:‘{"name":"手機","price":999}‘,10         success:function(data){//返回json結果11             alert(data);12         }13         14     });15 }
 1.4.1.2   controller
1 //請求json串(商品資訊),輸出json(商品資訊)2     //@RequestBody將請求的商品資訊的json串轉成itemsCustom對象3     //@ResponseBody將itemsCustom轉成json輸出4     @RequestMapping("/requestJson")5     public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom){6         7         //@ResponseBody將itemsCustom轉成json輸出8         return itemsCustom;9     }

 

1.4.1.3              測試結果

 

1.4.2     輸入key/value,輸出是json串1.4.2.1       jsp頁面

使用jquery的ajax提交key/value串,對輸出的json結果進行解析。

 1 //請求key/value,輸出是json 2 function responseJson(){ 3      4     $.ajax({ 5         type:‘post‘, 6         url:‘${pageContext.request.contextPath }/responseJson.action‘, 7         //請求是key/value這裡不需要指定contentType,因為預設就 是key/value類型 8         //contentType:‘application/json;charset=utf-8‘, 9         //資料格式是json串,商品資訊10         data:‘name=手機&price=999‘,11         success:function(data){//返回json結果12             alert(data.name);13         }14     });15 }
 1.4.2.2     controller
1 //請求key/value,輸出json2     @RequestMapping("/responseJson")3     public @ResponseBody ItemsCustom responseJson(ItemsCustom itemsCustom){4         5         //@ResponseBody將itemsCustom轉成json輸出6         return itemsCustom;7     }

 

1.4.2.3              測試

 

 

json資料交換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.