错误描述
最近在工作有个功能,希望能在接口中接收List</Long/>类型的数据,遇到一个问题,试了两种方式,分别报了如下错误:
No primary or default constructor found for interface java.util.List
Invalid character found in the request target [/xxx/xxx/query?packageIds[]=1349693322573
后面分析了原因都是因为参数传递不正确导致,特此来记录下。
get请求
- 使用Get方式,必须在参数上添加注解:@RequestParam(value = “ids”)
@GetMapping("/xxx/query")
@ApiOperationSupport(order = 4)
@ApiOperation(value = "xxxx", notes = "xxxx")
public R<XxxRespVO> query(@RequestParam(value = "ids") List<Long> ids) {
return xxx;
}
请求方式>>>get请求: ip:8080/xxx/query?ids=1&ids=2&ids=3
post请求
- 使用Post方式,必须在参数上添加注解:@RequestBody
@PostMapping("/xxx/query")
@ApiOperationSupport(order = 4)
@ApiOperation(value = "xxxx", notes = "xxxx")
public R<XxxRespVO> query(@RequestBody List<Long> ids) {
return xxx;
}
请求方式>>>get请求: ip:8080/xxx/query,在body中传递参数为:ids: [1, 2, 3 ],注意不是{ ids: [1, 2, 3 ] }