“JSON parse error: Cannot deserialize value of type `java.util.ArrayList<XXX>` from Object value ...

"JSON parse error: Cannot deserialize value of type `java.util.ArrayList` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList` 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 enclosureList;
}

可以看到enclosureList是一个集合形式的数据

给大家看下我用postman传参方式

“JSON parse error: Cannot deserialize value of type `java.util.ArrayList<XXX>` from Object value ..._第1张图片

错误传参方式:

{
    "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` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList` 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是一个集合,要在后面加上中括号

好了,这就是我解决以上错误的方法。。。

你可能感兴趣的:(Java,postman,java,json,开发语言)