赞
踩
"JSON parse error: Cannot deserialize value of type `java.util.ArrayList<xxx>` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<xxx>` from Object value (token `JsonToken.START_OBJECT`)\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 3, column: 22] (through reference chain: cn.com.sinosoft.entity.dto.EnclosureUploadDTO[\"enclosureList\"])",
今天周末加班改bug时,用postman传参遇到上述一个错误,搞了我十分钟。。。今天分享给大家避免踩坑
首先我的controller类如下
- // 上传附件
- @PostMapping("/upload")
- public R upload(@RequestBody EnclosureUploadDTO uploadDTO){
- internalProjectService.uploadEnclosure(uploadDTO);
- return R.ok();
- }
EnclosureUploadDTO里面有这几个参数
- @Data
- public class EnclosureUploadDTO {
- private String id;
-
- /**
- * 完成情况说明
- */
- private String completionDescription;
-
- private List<Enclosure> enclosureList;
- }
可以看到enclosureList是一个集合形式的数据
给大家看下我用postman传参方式
错误传参方式:
- {
- "id": "1590593194199277570",
- "enclosureList": {
- "enclosureName": "soft.pdf",
- "enclosureType": 21,
- "enclosureId": "1591262808073314362",
- "url": "www.baidu.com"
- }
- }
乍一看没啥问题啊,一执行的时候就报告错误
- {
-
- "code": 500,
-
- "msg": "JSON parse error: Cannot deserialize value of type `java.util.ArrayList<cn.com.sinosoft.entity.Enclosure>` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<cn.com.sinosoft.entity.Enclosure>` from Object value (token `JsonToken.START_OBJECT`)\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 3, column: 22] (through reference chain: cn.com.sinosoft.entity.dto.EnclosureUploadDTO[\"enclosureList\"])",
-
- "data": null
-
- }
通过错误提示最后一句话
(through reference chain: cn.com.sinosoft.entity.dto.EnclosureUploadDTO[\"enclosureList\"])"
大概能看出是我这个集合数据传参方式不对
正确的方式应该为:
- {
- "id": "1590593194199277570",
- "enclosureList": [{
- "name": "soft.pdf",
- "enclosureName": "soft.pdf",
- "enclosureType": 21,
- "enclosureId": "1591262808073314362",
- "url": "www.baidu.com"
- }]
- }
因为enclosureList是一个集合,要在后面加上中括号
好了,这就是我解决以上错误的方法。。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。