国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

springmvc json 數(shù)據(jù)交互

2019-11-14 08:54:36
字體:
供稿:網(wǎng)友
為什么要進行json數(shù)據(jù)交互json數(shù)據(jù)格式在接口調(diào)用中、HTML頁面中叫常用,json格式比較簡單,解析比較方便。比如:webservice接口,傳輸json數(shù)據(jù)sPRingmvc進行json交互@RequestBody作用:@RequestBody注解用于讀取http請求的內(nèi)容(字符串),通過springmvc提供的HttpMessageConverter接口將讀到的內(nèi)容轉(zhuǎn)換為json、xml等格式的數(shù)據(jù)并綁定到controller方法的參數(shù)上。@ResponseBody作用:該注解用于將Controller的方法返回的對象,通過HttpMessageConverter接口轉(zhuǎn)換為指定格式的數(shù)據(jù)如:json,xml等,通過Response響應給客戶端1、請求的是json,輸出json,要求請求的是json串,所以在前端頁面中需要將請求的內(nèi)容轉(zhuǎn)成json,不太方便。2、請求的是key/value,輸出json,此方法比較常用。準備環(huán)境Springmvc默認用MappingJacksonHttpMessageConverter對json數(shù)據(jù)進行轉(zhuǎn)換,需要加入jackson的包,如下:配置json轉(zhuǎn)換器在注解適配器中加入messageConverters
<!--注解適配器 -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">       <property name="messageConverters">       <list>       <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>       </list>       </property>    </bean> 注意:如果使用<mvc:annotation-driven />則不用定義上邊的內(nèi)容。實現(xiàn)jsp頁面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>在此處插入標題</title><script src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script><script type="text/Javascript">	// 請求json響應json	function requestJsonByJson() {		$.Ajax({					type : "post",					url : "${pageContext.request.contextPath }/requestJsonByJson.action",					contentType : "application/json;charset=utf-8",					data : '{"name":"測試用戶","age":99}',					success : function(data) {						alert(data);					}				});	}	// key=value請求響應json	function requestJsonByKeyValue() {		$.ajax({					type : "post",					url : "${pageContext.request.contextPath }/requestJsonByKeyValue.action",					//contentType默認是application/x-www-form-urlencoded//contentType : "application/json;charset=utf-8",					data : 'name=測試用戶&age=99',					success : function(data) {						alert(data);					}				});	}</script></head><body>	<input type="button" onclick="requestJsonByJson()" value="請求json響應json" />	<input type="button" onclick="requestJsonByKeyValue()" value="key=value請求響應json" /></body></html>Controller:
@Controllerpublic class TestJsonController {	// 提交json信息,響應json信息	@RequestMapping("/requestJsonByJson")	public @ResponseBody UserCustom requestJsonByJson(@RequestBody UserCustom userCustom) throws Exception {		return userCustom;	}	// 提交key value信息,響應json信息	@RequestMapping("/requestJsonByKeyValue")	public @ResponseBody UserCustom requestJsonByKeyValue(@RequestBody UserCustom userCustom) throws Exception {		return userCustom;	}}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 方正县| 始兴县| 通渭县| 广丰县| 满城县| 九龙县| 长垣县| 重庆市| 绥芬河市| 石泉县| 台北市| 灵寿县| 望都县| 葵青区| 尼木县| 洪雅县| 洪雅县| 兴山县| 休宁县| 安泽县| 承德市| 开江县| 营口市| 贵南县| 屏南县| 麟游县| 常山县| 平果县| 拜城县| 清徐县| 宁化县| 苍梧县| 阳信县| 葵青区| 湄潭县| 大邑县| 洪江市| 天津市| 贵阳市| 江门市| 丽江市|