[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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。