使用@RequestBody傳對象參數(shù)時碰到的坑
@RequestBody傳對象參數(shù)時碰到的坑
工作中需要使用到http接口傳一個對象數(shù)組,網(wǎng)上找到某博客:springmvc參數(shù)為對象,數(shù)組
但是測試還是不對,報錯:
2019-02-21 23:44:37.168 WARN 34133 --- [nio-7001-exec-6]
.w.s.m.s.DefaultHandlerExceptionResolver :
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error:
Can not construct instance of com.cainiao.cngdm.domain.common.Report: no String-argument constructor/factory method to deserialize from String value ('{"title":"11","note":"22","goodsList":[{"goodsNumber":"001","goodsName":"商品A"},{"goodsNumber":"002","goodsName":"商品B"}]}'); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.cainiao.cngdm.domain.common.Report: no String-argument constructor/factory method to deserialize from String value ('{"title":"11","note":"22","goodsList":[{"goodsNumber":"001","goodsName":"商品A"},{"goodsNumber":"002","goodsName":"商品B"}]}')
看樣子好像時說少了string參數(shù)的構(gòu)造函數(shù),試過很多其他辦法還不行后,決定試一下增加一個String類型的構(gòu)造方法,代碼如下:
public class Report implements Serializable {
private static final long serialVersionUID = 1L;
String title;
String note;
List<Goods> goodsList;
Report(){}
Report(String dd) {
Report report = JSON.parseObject(dd,Report.class);
this.title=report.title;
this.note=report.title;
this.goodsList=report.goodsList;
}
//getter... setter...
}
public class Goods implements Serializable {
private static final long serialVersionUID = 1L;
String goodsNumber;
String goodsName;
//getter... setter...
}
PS:@RequestBody的使用需要加載MappingJackson2HttpMessageConverter,但是SpringBoot的官方文檔提到,這個是默認已經(jīng)加載的了,所以最后spring的配置這部分我又刪了,測試OK。
上面是測試代碼,測試代碼測通后,我的代碼還是報錯
我的真實代碼是這樣的:
@RequestMapping("/addReportDo")
@ResponseBody
public String addReportDo(String title,String note,@RequestBodyList<Goods> goodsList){
System.out.println(report);
return "ok";
}
//錯誤代碼
2019-02-22 12:05:28.498 WARN 36431 --- [nio-7001-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message:
org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token;
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
大概意思是不能從這個String轉(zhuǎn)到List,也就是list對應(yīng)jsonArray格式不對,使用對象像上面一樣封裝起來Report就OK了。
也就是下面的區(qū)別:
{goodsList: [{
goodsNumber: "001",
goodsName: "商品A"
},{
goodsNumber: "002",
goodsName: "商品B"
}]
}
{[{
goodsNumber: "001",
goodsName: "商品A"
},{
goodsNumber: "002",
goodsName: "商品B"
}]
}
由于時間原因,只是記錄一下現(xiàn)象和解決方案,具體原因還沒有細看springMVC里的json處理是怎么進行的。
@RequestBody 的正確使用
@RequestBody接收的是一個Json對象
一直在調(diào)試代碼都沒有成功,后來發(fā)現(xiàn),其實 @RequestBody接收的是一個Json對象的字符串,而不是一個Json對象。然而在ajax請求往往傳的都是Json對象,后來發(fā)現(xiàn)用 JSON.stringify(data)的方式就能將對象變成字符串。
同時ajax請求的時候也要指定dataType: "json",contentType:"application/json" 這樣就可以輕易的將一個對象或者List傳到Java端,使用@RequestBody即可綁定對象或者List.
@RequestBody的使用
需要加載MappingJackson2HttpMessageConverter,但是SpringBoot的官方文檔提到,這個是默認已經(jīng)加載的了,而且json字符串和javabean也沒有書寫的錯誤
直接通過瀏覽器輸入url時,@RequestBody獲取不到j(luò)son對象,需要用java編程或者基于ajax的方法請求,將Content-Type設(shè)置為application/json
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用Thymeleaf模板引擎訪問靜態(tài)html的過程
這篇文章主要介紹了SpringBoot使用Thymeleaf模板引擎訪問靜態(tài)html的過程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
Spring?Boot整合Log4j2.xml的問題及解決方法
這篇文章主要介紹了Spring?Boot整合Log4j2.xml的問題,本文給大家分享解決方案,需要的朋友可以參考下2023-09-09

