如何在邮递员中发送包含数组列表的对象

Posted

技术标签:

【中文标题】如何在邮递员中发送包含数组列表的对象【英文标题】:How to send Object which contains an arraylist in the postman 【发布时间】:2020-10-25 23:32:07 【问题描述】:

我正在用 springboot rest API 做一个项目。 我的控制器类如下所示,

@RequestMapping(value = "/artdimfil", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
    @ResponseBody
    public boolean saveArticleDimFilter(@RequestBody ArtDimFil artDimFil)

我想在邮递员中发送以下有效负载,


    "id": "b30d1486-04c8-4acc-8d14-8604c81accab",
    "docIdentifier": 
        "recType": "ABC",
        "accesslist": [
         
        "userid": "xyz@abc.com",
        "role": "abc",
        "status": "Active",
        "actiondate": "2020-07-03T17:59:34.600+0000",
        "name": "XYZ",
      ]
    

但它给出了错误提示

[org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.ibm.epm.next.model.Accesslist` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.ibm.epm.next.model.Accesslist` out of START_ARRAY token.

请建议我该怎么做。

【问题讨论】:

请修复 json 格式并显示 ArtDimFil 的外观 @a4dev92 : 更正了 json 格式。ArtDimFil 看起来像这样,@JsonProperty("_id") private String _id; @JsonProperty("docIdentifier") private DocIdentifier docIdentifier; @JsonProperty("dimfilDocData") private DimfilDocData dimfilDocData; 您有一个“Accesslist”对象,但您需要一个对象数组:List。这是反序列化 Json 数组的正确方法 【参考方案1】:

我认为你应该删除“XYZ”之后的逗号

   
    "userid": "xyz@abc.com",
    "role": "abc",
    "status": "Active",
    "actiondate": "2020-07-03T17:59:34.600+0000",
    "name": "XYZ",
  

还可以很好地显示 docIdentifier 类的外观并显示 accesslist 中的类

【讨论】:

那个逗号是打错了。 DocIdentifier 和 Accesslist 类是 POJO。这是DocIdentifier类的样子,@JsonProperty("rec_type") private String recType; @JsonProperty("accesslist") private List accesslist = new ArrayList();这是 Accesslist 类的外观,@JsonProperty("userid") private String userid; @JsonProperty("role") 私有字符串角色; @JsonProperty("status") 私有字符串状态; @JsonProperty("actiondate") 私有字符串 actiondate; @JsonProperty("name") 私有字符串名称;【参考方案2】:

它试图反序列化为“AccessList”-但我认为它没有使用您的“AccessList”,因为错误显示: 无法反序列化 com.ibm.epm.next.model.Accesslist 的实例

我建议你重命名你的类,或者确保你有正确的导入语句。您应该导入自己的“AccessList”类。

【讨论】:

谢谢它现在正在工作,但现在还有另一个问题..我在调试时看不到 Eclipse 中的 accessList 数据。数据应该在 accessList 数组中吗?我没看到。你有什么主意吗? “用户 ID”:“xyz@abc.com”、“角色”:“abc”、“状态”:“活动”、“操作日期”:“2020-07-03T17:59:34.600+0000”、“名称”: “XYZ” 你能展示你的“Accesslist”课程吗? List 听起来像 List。不确定这是否有问题。 AccessList 是一个 setter/getter 方法。 @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder( "userid", "role", "status", "actiondate", "name" ) public class AccessList @JsonProperty("userid") private String userid; @JsonProperty("role") 私有字符串角色; @JsonProperty("status") 私有字符串状态; @JsonProperty("actiondate") 私有字符串 actiondate; @JsonProperty("name") 私有字符串名称;私有字符串 __caseId; 嗯,没有更多信息,我无法帮助你。但是,如果它解决了您最初的问题,请随时投票/接受我的回答。如果您需要进一步的帮助,请提出一个新问题,其中包含有关 Eclipse 中未显示的列表的详细信息。

以上是关于如何在邮递员中发送包含数组列表的对象的主要内容,如果未能解决你的问题,请参考以下文章

如何使用邮递员发布对象和列表

如何在邮递员中将数据作为@RequestPart 发送

如何从邮递员查询参数c#发送值列表

如何从邮递员传递字节数组

我们如何在DTO中使用对象数组和nestjs中的multipart-formdata?

邮递员表单数据发送带有文件的复杂对象