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

首頁 > 學院 > 開發設計 > 正文

springmvc json 數據交互

2019-11-11 06:12:49
字體:
來源:轉載
供稿:網友
為什么要進行json數據交互json數據格式在接口調用中、HTML頁面中叫常用,json格式比較簡單,解析比較方便。比如:webservice接口,傳輸json數據sPRingmvc進行json交互@RequestBody作用:@RequestBody注解用于讀取http請求的內容(字符串),通過springmvc提供的HttpMessageConverter接口將讀到的內容轉換為json、xml等格式的數據并綁定到controller方法的參數上。@ResponseBody作用:該注解用于將Controller的方法返回的對象,通過HttpMessageConverter接口轉換為指定格式的數據如:json,xml等,通過Response響應給客戶端1、請求的是json,輸出json,要求請求的是json串,所以在前端頁面中需要將請求的內容轉成json,不太方便。2、請求的是key/value,輸出json,此方法比較常用。準備環境Springmvc默認用MappingJacksonHttpMessageConverter對json數據進行轉換,需要加入jackson的包,如下:配置json轉換器在注解適配器中加入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 />則不用定義上邊的內容。實現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;	}}
上一篇:網絡基礎

下一篇:LeetCode Jump Game II

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 句容市| 清镇市| 紫金县| 孟村| 康马县| 凌源市| 独山县| 大城县| 乾安县| 嘉禾县| 浦县| 中江县| 洛阳市| 上杭县| 密云县| 县级市| 阜平县| 南充市| 金溪县| 日土县| SHOW| 咸丰县| 石门县| 肃南| 田林县| 沈丘县| 句容市| 辽宁省| 吉木萨尔县| 全椒县| 普宁市| 大安市| 景德镇市| 庆元县| 万荣县| 西充县| 资源县| 天水市| 黑河市| 西藏| 玉龙|