[2019/05/17]解决springboot测试List接口时JSON传参异常

报错信息,大致如下

c.c.c.c.a.BaseControllerExceptionHandler : 运行时异常:
java.lang.IllegalStateException: No primary or default constructor found for interface java.util.List
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:212)
......
WARN 18472 --- [nio-9090-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: java.lang.IllegalStateException: No primary or default constructor found for interface java.util.List

再次修改后仍又报错:

org.springframework.http.converter.
HttpMessageNotReadableException: Required request body is missing:

(以上报错信息谷歌翻译了一遍,大致知道了是属性接收异常和http消息转换异常)

原因 : 接口不规范, 后端一时快, 前端两行泪
第二次报错原因, @RequestBody这个标签在post 、put 方法中用于接收json格式的数据 ,get方法不行! get方法不行!
第一次报错原因, @RequestBody用来接收一个List当做整体, 不要用@RequestParam--JSON格式的list没法实现对应了, 也不要错误写成@Param~~
规范写法
    @ApiOperation(value = "批量删除")
    @PostMapping(value = "delBatchIds")
    public Boolean delBatchIds(@RequestBody List<Long> ids){
        return xxxxxService.deleteBatchIds(ids);
    }
再次测试,OK

image.png
image.png

posted @ 2019-06-19 17:25  山枫叶纷飞  阅读(1988)  评论(0编辑  收藏  举报