当前位置: 首页 > 知识库问答 >
问题:

无法反序列化“DataRequests”dto中START_ARRAY标记外的java.util.LinkedHashMap实例

袁志专
2023-03-14

我有一个高度嵌套的api响应:

{
"dataRequests": [
    {
        "status": "success",
        "title": "token",
        "values": {
            "limit": 1,
            "offset": 0,
            "count": 1,
            "total": 1,
            "elements": [
                {
                    "type": "DOMAIN",
                    "permission": "default",
                    "properties": [
                        {
                            "name": "property:id",
                            "value": 390
                        },
                        {
                            "name": "setting:crawler:token",
                            "value": "here's a token"
                        }
                    ],
                    "filters": []
                }
            ]
        }
    }
]
 }

我试图通过使用许多包装来解析它。因为当我想获得带令牌的字段'value'时,它只从一个dataRequest dto返回'null'。

    Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize 
  instance of java.util.LinkedHashMap out of START_ARRAY token
  at [Source: {"dataRequests":[{"status":"success","title":"token","values": 
   {"limit":1,"offset":0,"count":1,"total":1,"elements": 
  [{"type":"DOMAIN","permission":"default","properties":[{"name":"property:id","value":390}, 
  {"name":"setting:crawler:token","value":"here's a token"}],"filters":[]}]}}]}; line: 1, column: 
      17] (through reference chain: com.rest.dto.ProjectTokenDto["dataRequests"])
@Data
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProjectTokenDto {

    private Map<String, ValuesDto> dataRequests;

}
@Data
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class ValuesDto {

    private Map<String, List<ElementsDto>> values;
}

元素DTO:

@Data
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class ElementsDto {

    private Map<String, List<PropertiesDto>> elements;
}

属性DTO:

@Data
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class PropertiesDto {

    private String name;
    private String value;
}

然后我试着用不是最好的方式解析这棵树,但是我无法理解如何更容易地解析它?并获取上面的异常:

ProjectTokenDto wrapper = graphQLSteps.postProjectToken(id);

        Map<String, ValuesDto> values = wrapper.getDataRequests();

        Map<String, List<ElementsDto>> elements = new HashMap<>();

        Map<String, List<PropertiesDto>> properties = new HashMap<>();

        for (Map.Entry<String, ValuesDto> entry : values.entrySet()) {
            if (entry.getKey().equals("values")) {
                elements.put(entry.getKey(), (List<ElementsDto>) entry.getValue().getValues());
            }
        }

        for (Map.Entry<String, List<ElementsDto>> entry : elements.entrySet()) {
            if (entry.getKey().equals("elements")) {
                properties.put(entry.getKey(), (List<PropertiesDto>) entry.getValue().get(0).getElements());
            }
        }

        for (Map.Entry<String, List<PropertiesDto>> entry : properties.entrySet()) {
            if (!entry.getValue().get(0).getValue().equals(Long.toString(id))) {
                token = entry.getValue().get(0).getValue();
            }
        } 
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_NUMBER_INT token
 at [Source: {"dataRequests":[{"status":"success","title":"token","values":{"limit":1,"offset":0,"count":1,"total":1,"elements":[{"type":"DOMAIN","permission":"default","properties":[{"name":"property:id","value":390},{"name":"setting:crawler:token","value":"here's a token"}],"filters":[]}]}}]}; line: 1, column: 72] (through reference chain: com.rest.dto.ProjectTokenDto["dataRequests"]->java.util.ArrayList[0]->com.rest.dto.ValuesDto["values"]->java.util.LinkedHashMap["limit"])

试图通过将JsonIgnoreProperties更改为

@JsonIgnoreProperties({"limit", "offset", "count", "total", "status", "title"})

在valuesDto中,但最终具有相同的异常

共有1个答案

元彦君
2023-03-14

我通过更改DTO的值来修复它:

ProjectTokendto:

private List<DataRequestDto> dataRequests;

DataRequestSDTo:

private ValuesDto values;
private List<ElementsDto> elements;
private List<PropertiesDto> properties;

最后是PropertiesDTO:

private String name;
private String value;
 类似资料: