SpringMVC教程之json交互使用詳解
json數(shù)據(jù)交互
1.1 @RequestBody
作用:@RequestBody注解用于讀取http請求的內(nèi)容(字符串),通過springmvc提供的HttpMessageConverter接口將讀到的內(nèi)容轉(zhuǎn)換為json、xml等格式的數(shù)據(jù)并綁定到controller方法的參數(shù)上。
本例子應(yīng)用:@RequestBody注解實(shí)現(xiàn)接收http請求的json數(shù)據(jù),將json數(shù)據(jù)轉(zhuǎn)換為Java對象
1.2 @ResponseBody
作用:該注解用于將Controller的方法返回的對象,通過HttpMessageConverter接口轉(zhuǎn)換為指定格式的數(shù)據(jù)如:json,xml等,通過Response響應(yīng)給客戶端
本例子應(yīng)用:@ResponseBody注解實(shí)現(xiàn)將controller方法返回對象轉(zhuǎn)換為json響應(yīng)給客戶端
1.3 請求json,響應(yīng)json實(shí)現(xiàn):
1.3.1 環(huán)境準(zhǔn)備
Springmvc默認(rèn)用MappingJacksonHttpMessageConverter對json數(shù)據(jù)進(jìn)行轉(zhuǎn)換,需要加入jackson的包,如下:
1.3.2 配置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)容。
1.3.3 controller編寫
// 商品修改提交json信息,響應(yīng)json信息
@RequestMapping("/editItemSubmit_RequestJson")
public @ResponseBody Items editItemSubmit_RequestJson(@RequestBody Items items) throws Exception {
System.out.println(items);
//itemService.saveItem(items);
return items;
}
1.3.4 頁面js方法編寫:
引入 js:
<script type="text/JavaScript"
src="${pageContext.request.contextPath }/js/jQuery-1.4.4.min.js"></script>
//請求json響應(yīng)json
function request_json(){
$.ajax({
type:"post",
url:"${pageContext.request.contextPath }/item/editItemSubmit_RequestJson.action",
contentType:"application/json;charset=utf-8",
data:'{"name":"測試商品","price":99.9}',
success:function(data){
alert(data);
}
});
}
1.4 Form提交,響應(yīng)json實(shí)現(xiàn):
采用form提交是最常用的作法,通常有post和get兩種方法,響應(yīng)json數(shù)據(jù)是為了方便客戶端處理,實(shí)現(xiàn)如下:
1.4.1 環(huán)境準(zhǔn)備
同第一個(gè)例子
1.4.2 controller編寫
// 商品修改提交,提交普通form表單數(shù)據(jù),響應(yīng)json
@RequestMapping("/editItemSubmit_ResponseJson")
public @ResponseBody Items editItemSubmit_ResponseJson(Items items) throws Exception {
System.out.println(items);
//itemService.saveItem(items);
return items;
}
1.4.3 頁面js方法編寫:
function formsubmit(){
var user = " name=測試商品&price=99.9";
alert(user);
$.ajax(
{
type:'post',//這里改為get也可以正常執(zhí)行
url:'${pageContext.request.contextPath}/item/ editItemSubmit_RequestJson.action',
//ContentType沒指定將默認(rèn)為:application/x-www-form-urlencoded
data:user,
success:function(data){
alert(data.name);
}
}
)
}
從上邊的js代碼看出,已去掉ContentType的定義,ContentType默認(rèn)為:application/x-www-form-urlencoded格式。
1.4.4 jquery的form插件插件
針對上邊第二種方法,可以使用jquery的form插件提交form表單,實(shí)現(xiàn)ajax提交form表單,如下:
引用js:
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery.form.min.js"></script>
js方法如下:
function response_json() {
//form對象
var formObj = $("#itemForm");
//執(zhí)行ajax提交
formObj.ajaxSubmit({
dataType : "json",//設(shè)置預(yù)期服務(wù)端返回json
success : function(responseText) {
alert(responseText);
}
});
}
1.4.5 小結(jié)
實(shí)際開發(fā)中常用第二種方法,請求key/value數(shù)據(jù),響應(yīng)json結(jié)果,方便客戶端對結(jié)果進(jìn)行解析。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SPRINGMVC JSON數(shù)據(jù)交互如何實(shí)現(xiàn)
- 詳解springmvc之json數(shù)據(jù)交互controller方法返回值為簡單類型
- 詳解springMVC之與json數(shù)據(jù)交互方法
- springmvc實(shí)現(xiàn)json交互-requestBody和responseBody
- Spring mvc實(shí)現(xiàn)Restful返回json格式數(shù)據(jù)實(shí)例詳解
- Spring mvc實(shí)現(xiàn)Restful返回xml格式數(shù)據(jù)實(shí)例詳解
- Springmvc restful配置遇到的小坑
- SpringMVC restful 注解之@RequestBody進(jìn)行json與object轉(zhuǎn)換
- SpringMVC JSON數(shù)據(jù)交互及RESTful支持實(shí)現(xiàn)方法
相關(guān)文章
Java 改造ayui表格組件實(shí)現(xiàn)多重排序
layui 的表格組件目前只支持單列排序,在實(shí)際應(yīng)用中并不能很好的支撐我們的業(yè)務(wù)需求。今天一時(shí)手癢,決定改造一番以支持多重排序。2021-04-04
spring 自動注入AutowiredAnnotationBeanPostProcessor源碼解析
這篇文章主要介紹了spring自動注入AutowiredAnnotationBeanPostProcessor源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
完美解決springboot項(xiàng)目出現(xiàn)”java: 錯誤: 無效的源發(fā)行版:17“問題(圖文詳解)
這篇文章主要介紹了完美解決springboot項(xiàng)目出現(xiàn)”java: 錯誤: 無效的源發(fā)行版:17“問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Springboot+AOP實(shí)現(xiàn)返回?cái)?shù)據(jù)提示語國際化的示例代碼
這篇文章主要介紹了Springboot+AOP實(shí)現(xiàn)返回?cái)?shù)據(jù)提示語國際化的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
線程池中使用spring aop事務(wù)增強(qiáng)
這篇文章主要介紹了線程池中使用spring aop事務(wù)增強(qiáng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
SpringBoot連接Hive實(shí)現(xiàn)自助取數(shù)的示例
這篇文章主要介紹了SpringBoot連接Hive實(shí)現(xiàn)自助取數(shù)的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12
feign調(diào)用第三方接口,編碼定義GBK,響應(yīng)中文亂碼處理方式
這篇文章主要介紹了feign調(diào)用第三方接口,編碼定義GBK,響應(yīng)中文亂碼處理方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
mybatis-plus樂觀鎖實(shí)現(xiàn)方式詳解
這篇文章主要介紹了mybatis-plus樂觀鎖實(shí)現(xiàn)方式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

