1、json數據交互:javascript
json數據格式在接口調用中、html頁面中較經常使用,json格式比較簡單,解析還比較方便。html
好比:webservice接口,傳輸json數據.前端
springMVC進行json交互java
1)環境準備:jquery
加載json轉換的jar包:web
springmvc中使用jackson的包進行json轉換(@requestBody和@responseBody使用下邊的包進行json轉)ajax
jackson-core-asl-1.9.11.jarspring
jackson-mapper-asl-1.9.11.jar數據庫
2)配置json轉換器;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>
若是使用<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 @Controller 17 public class JsonTest { 18 19 @Autowired 20 private ItemsService itemsService; 21 22 //請求json串(商品信息),輸出json(商品信息) 23 //@RequestBody將請求的商品信息的json串轉成itemsCustom對象 24 @RequestMapping("/requestJson") 25 @ResponseBody 26 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,輸出json 33 //@ResponseBody將itemsCustom轉成json輸出 34 @RequestMapping("/responseJson") 35 @ResponseBody 36 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,輸出是json 11 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,輸出是json 27 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>
測試結果:
數據庫內容:
requestJson:
responseJson:
2、RESTful支持:
RESTful介紹:
RESTful(即Representational State Transfer的縮寫)實際上是一個開發理念,是對http的很好的詮釋。
一、對url進行規範,寫RESTful格式的url
非REST的url:http://...../queryItems.action?id=001&type=T01
REST的url風格:http://..../items/001
特色:url簡潔,將參數經過url傳到服務端
二、http的方法規範
無論是刪除、添加、更新。。使用url是一致的,若是進行刪除,須要設置http的方法爲delete,同理添加put。。。
後臺controller方法:判斷http方法,若是是delete執行刪除,若是是post執行更新。
三、對http的contentType規範
請求時指定contentType,要json數據,設置成json格式的type。。
下面是RESTful的例子:這個例子主要是對url進行了規範:
1)ItemsController:
定義方法,進行url映射使用REST風格的url,將查詢商品信息的id傳入controller .
@Controller @RequestMapping("/items") public class ItemsController { @Autowired private ItemsService itemsService; //查詢商品信息,輸出json ///itemsView/{id}裏邊的{id}表示佔位符,經過@PathVariable獲取佔位符中的參數, //若是佔位符中的名稱和形參名一致,在@PathVariable能夠不指定名稱 @RequestMapping("/itemsView/{id}") @ResponseBody public ItemsCustom itemsView(@PathVariable("id") Integer itemId)throws Exception{ ItemsCustom itemsCustom = itemsService.findItemsById(itemId); return itemsCustom; } .... }
2)REST風格的前端控制器配置、靜態資源文件配置:
因爲REST風格是url是不帶後綴的,這裏配置url-pattern爲/,亦須要配置靜態文件不讓dispatcherServlert解析:
web.xml中配置:
1 <!-- springmvc前端控制器,rest配置 --> 2 <servlet> 3 <servlet-name>springmvc_rest</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:spring/springmvc.xml</param-value> 8 </init-param> 9 </servlet> 10 <servlet-mapping> 11 <servlet-name>springmvc_rest</servlet-name> 12 <url-pattern>/</url-pattern> 13 </servlet-mapping>
springmvc中配置訪問靜態資源:
<mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:resources mapping="/js/**" location="/js/"/>
訪問結果: