springMvc与Json数据的交互
json数据格式在接口调用, html页面中较为常用, json格式比较简单, 解析还比较方便, 比如: webservice接口, 传输json数据
(1) 请求的是json串, 必须在客户端将数据转换成json串再向后台发出请求, 输出json串, 这种因为在前端处理json, 不方便
(2) 请求的不是json串, 输出json串, 建议使用这种
环境准备: [jar包一定要加全]
jar包: jackson-annotations-2.6.0-xh.jar, jackson-core-2.6.0-xh.jar, jackson-databind-2.6.0-xh.jar, jackson-jr-all-2.4.3-xh.jar
配置json转换器: 如果springmvc.xml中配置的是<mvc:annotation-driven/>注解驱动, 则不需要配置以下的json转换器
在注解适配器中配置messageConverters
1 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 2 <property name="messageConverters"> 3 <list> 4 <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean> 5 </list> 6 </property> 7 </bean>
[测试]
(1) 输入json串, 输出json串: 使用jquery的ajax发送json数据, 在页面解析json数据, contentType="application/json", 需要指定
jsp:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <base href="<%=basePath%>"> 13 <title>json数据交互测试</title> 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 20 <script type="text/javascript" src="${pageContext.request.contextPath }/script/js/jquery-1.9.1.js"></script> 21 <script type="text/javascript"> 22 // 请求json, 输出json 23 function requestJson() { 24 $.ajax({ 25 type: 'post', 26 url: '${pageContext.request.contextPath }/requestJson.action', 27 data: '{"name":"手机","price":999}', 28 contentType: 'application/json;charset=utf-8', 29 success: function(data) { 30 alert(data.name + " | " + data.price); 31 } 32 }); 33 }39 </script> 40 </head> 41 42 <body> 43 <input type="button" value="请求json, 输出json" onclick="requestJson();"/> 45 </body> 46 </html>
Handler
1 package com.itcast.ssm.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestBody; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 8 import com.itcast.ssm.po.ItemsCustom; 9 10 @Controller 11 public class RequestJson { 12 13 /** 14 * 请求json(商品信息), 响应json(商品信息) 15 * @RequestBody 将请求的商品的json串转成ItemsCustom 16 * @ResponseBody 将ItemsCustom转成json输出 17 */ 18 @RequestMapping("/requestJson") 19 public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom) { 20 21 // @ResponseBody, 将ItemsCustom转成json输出 22 return itemsCustom; 23 } 24 }
(2) 输入key/value, 输出json串, contentType="application/x-www-form-urlencoded", 默认情况, 不需要指定
jsp
1 <input type="button" value="请求key/value, 输出json" onclick="responseJson();"/>
js
1 // 请求key/value, 输出json 2 function responseJson() { 3 $.ajax({ 4 type: 'post', 5 url: '${pageContext.request.contextPath }/responseJson.action', 6 // 数据是key/value格式 7 data: 'name=手机&price=999', 8 // 发送的key/value数据, 默认类型是contentType="application/x-www-form-urlencoded"[不需要写] 9 success: function(data) { 10 alert(data.name + " | " + data.price); 11 } 12 }); 13 }
handler
1 // 请求key/value格式, 响应json格式 2 @RequestMapping("/responseJson") 3 public @ResponseBody ItemsCustom responseJson(ItemsCustom itemsCustom) { 4 5 // @ResponseBody, 将ItemsCustom转成json输出 6 return itemsCustom; 7 }
测试:
(1) 请求json, 响应json
(2) 请求key/value, 响应json