解决SpringMVC报错Can not deserialize instance of java.util.ArrayList out of START_OBJECT token问题

SpringMVC请求报错Can not deserialize instance of java.util.ArrayList out of START_OBJECT token.

请求参数是json格式:

{
	"busiOrderNoList": ["ord1526358029166", "ord1526358060543"]
}

接收是POST 方法:


 @ApiOperation(tags = {Constants.API_RequiresNoPermission}, value = "查询支付结果", notes = "### 查询支付结果")
 @RequestMapping(value = "/findTradeInfoForBusiOrderId" ,method = RequestMethod.POST)
 public PayrollTradenoPay2CResult findTradeInfoForBusiOrderId((@RequestBody List paramList)){
        return payrollTradenoService.findTradeInfoForBusiOrderId(paramList);
 }

这是明显不行的.如果要接收List,应该更改前端传参.直接去掉大括号和参数名.如下

["ord1526358029166", "ord1526358060543"]

这样就能转换为List了.


这有一个文章可以参考:SpringMVC获取前端传来的json数据的4种方式:

我把它整理了一下:
1、以RequestParam接收
前端传来的是json数据不多时:[id:id],可以直接用@RequestParam来获取值


@Autowired
private AccomodationService accomodationService;

@RequestMapping(value = "/update")
@ResponseBody
public String updateAttr(@RequestParam ("id") int id) {
    int res=accomodationService.deleteData(id);
    return "success";
}
 


2、以实体类方式接收
前端传来的是一个json对象时:{【id,name】},可以用实体类直接进行自动绑定

@Autowired
private AccomodationService accomodationService;

@RequestMapping(value = "/add")
@ResponseBody
public String addObj(@RequestBody Accomodation accomodation) {
       this.accomodationService.insert(accomodation);
       return "success";
}


3、以Map接收
前端传来的是一个json对象时:{【id,name】},可以用Map来获取

@Autowired
private AccomodationService accomodationService;

@RequestMapping(value = "/update")
@ResponseBody
public String updateAttr(@RequestBody Map map) {
    if(map.containsKey("id"){
        Integer id = Integer.parseInt(map.get("id"));
    }
    if(map.containsKey("name"){
        String objname = map.get("name").toString();
    }
    // 操作 ...
    return "success";
}


4、以List接收
当前端传来这样一个json数组:[{id,name},{id,name},{id,name},...]时,用List接收

@Autowired
private AccomodationService accomodationService;

@RequestMapping(value = "/update")
@ResponseBody
public String updateAttr(@RequestBody List list) {
    for(Accomodation accomodation:list){
        System.out.println(accomodation.toString());
    }
    return "success";
}

 

你可能感兴趣的:(Java,SpringMVC,Spring)