亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用@RequestBody傳對象參數(shù)時(shí)碰到的坑

 更新時(shí)間:2021年08月20日 08:58:21   作者:青魚入云  
這篇文章主要介紹了使用@RequestBody傳對象參數(shù)時(shí)碰到的坑,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

@RequestBody傳對象參數(shù)時(shí)碰到的坑

工作中需要使用到http接口傳一個(gè)對象數(shù)組,網(wǎng)上找到某博客:springmvc參數(shù)為對象,數(shù)組

但是測試還是不對,報(bào)錯(cuò):

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"}]}')

看樣子好像時(shí)說少了string參數(shù)的構(gòu)造函數(shù),試過很多其他辦法還不行后,決定試一下增加一個(gè)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的官方文檔提到,這個(gè)是默認(rèn)已經(jīng)加載的了,所以最后spring的配置這部分我又刪了,測試OK。

上面是測試代碼,測試代碼測通后,我的代碼還是報(bào)錯(cuò)

我的真實(shí)代碼是這樣的:

 @RequestMapping("/addReportDo")
    @ResponseBody
    public String addReportDo(String title,String note,@RequestBodyList<Goods> goodsList){
        System.out.println(report);
        return "ok";
    }

//錯(cuò)誤代碼
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

大概意思是不能從這個(gè)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"
 }]
}

由于時(shí)間原因,只是記錄一下現(xiàn)象和解決方案,具體原因還沒有細(xì)看springMVC里的json處理是怎么進(jìn)行的。

@RequestBody 的正確使用

@RequestBody接收的是一個(gè)Json對象

一直在調(diào)試代碼都沒有成功,后來發(fā)現(xiàn),其實(shí) @RequestBody接收的是一個(gè)Json對象的字符串,而不是一個(gè)Json對象。然而在ajax請求往往傳的都是Json對象,后來發(fā)現(xiàn)用 JSON.stringify(data)的方式就能將對象變成字符串。

同時(shí)ajax請求的時(shí)候也要指定dataType: "json",contentType:"application/json" 這樣就可以輕易的將一個(gè)對象或者List傳到Java端,使用@RequestBody即可綁定對象或者List.

@RequestBody的使用

需要加載MappingJackson2HttpMessageConverter,但是SpringBoot的官方文檔提到,這個(gè)是默認(rèn)已經(jīng)加載的了,而且json字符串和javabean也沒有書寫的錯(cuò)誤

直接通過瀏覽器輸入url時(shí),@RequestBody獲取不到j(luò)son對象,需要用java編程或者基于ajax的方法請求,將Content-Type設(shè)置為application/json

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論