標籤:ems java 傳輸 技術 ret row 調用 方便 com
一、json資料互動:
json資料格式在介面調用中、html頁面中較常用,json格式比較簡單,解析還比較方便。
比如:webservice介面,傳輸json資料.
springMVC進行json互動
1)環境準備:
載入json轉換的jar包:
springmvc中使用jackson的包進行json轉換(@requestBody和@responseBody使用下邊的包進行json轉)
jackson-core-asl-1.9.11.jar
jackson-mapper-asl-1.9.11.jar
2)配置json轉換器;
如果是配置單個的註解適配器,要加入下面配置:
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>
View Code
如果使用<mvc:annotation-driven /> 則不用定義上邊的內容。
3)代碼實現:測試:
1 package com.cy.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestBody; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 9 import com.cy.po.ItemsCustom;10 import com.cy.service.ItemsService;11 12 /**13 * json互動測試14 *15 */16 @Controller17 public class JsonTest {18 19 @Autowired20 private ItemsService itemsService;21 22 //請求json串(商品資訊),輸出json(商品資訊)23 //@RequestBody將請求的商品資訊的json串轉成itemsCustom對象24 @RequestMapping("/requestJson")25 @ResponseBody26 public ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom) throws Exception{27 int id = itemsCustom.getId();28 ItemsCustom itemsCustom2 = itemsService.findItemsById(id);29 return itemsCustom2;30 }31 32 //請求key/value,輸出json33 //@ResponseBody將itemsCustom轉成json輸出34 @RequestMapping("/responseJson")35 @ResponseBody36 public ItemsCustom responseJson(ItemsCustom itemsCustom) throws Exception{37 int id = itemsCustom.getId();38 ItemsCustom itemsCustom2 = itemsService.findItemsById(id);39 return itemsCustom2;40 }41 }
jsp頁面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>json互動測試</title> 8 <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script> 9 <script type="text/javascript">10 //請求json,輸出是json11 function requestJson(){12 $.ajax({13 type:‘post‘,14 url:‘${pageContext.request.contextPath }/requestJson.action‘,15 contentType:‘application/json;charset=utf-8‘,16 //資料格式是json串,商品資訊17 //這邊很嚴格的做了測試,必須是這種格式;18 //key加引號;整個{}加引號;整個就是一個json串;有點像JOSN.stringify(JOSNObject)這樣子19 data:‘{"id":1,"name":"手機","price":999}‘,20 success:function(data){21 console.log(data);22 }23 24 });25 }26 //請求key/value,輸出是json27 function responseJson(){28 $.ajax({29 type:‘post‘,30 url:‘${pageContext.request.contextPath }/responseJson.action‘,31 data:‘id=4&name=手機&price=999‘,32 success:function(data){//返回json結果33 console.log(data);34 }35 });36 }37 </script>38 </head>39 <body>40 <input type="button" onclick="requestJson()" value="請求json,輸出是json"/>41 <input type="button" onclick="responseJson()" value="請求key/value,輸出是json"/>42 </body>43 </html>View Code
測試結果:
資料庫內容:
requestJson:
responseJson:
二、RESTful支援:
springMVC學習(11)-json資料互動和RESTful支援