ObjectMapper映射不能正确读取List<Map&gt的数据。

问题描述 投票:0回答:1

我有一个 DataResponseDto.json

{
  "data": [
    {
      "customRule": {
        "code": null,
        "executionType": "ON_SUCCESS",
        "description": "Description",
        "owners": null,
        "type": "TWO",
        "enabled": true,
        "objectType": "TEST",
        "syncObjectKPIs": null,
        "inactive": false,
        "responsible": null,
        "id": "0AB58A47D3A64B56A6B74DA0E66935DD",
        "embedded": true,
        "value": null,
        "variables": [],
        "kafkaEventName": null,
        "lastChanged": 1530091858490,
        "createPerson": null,
        "externalId": null,
        "groups": null,
        "eventType": "UPDATE",
        "branches": null,
        "executionOrder": null,
        "createDateTime": null,
        "cronExpression": null,
        "udfMetaGroups": null,
        "name": "Sample1",
        "location": null,
        "permissionsType": "USER",
        "udfValues": null,
        "conditions": null,
        "actions": [
          {
            "name": "ChecklistInstance",
            "parameters": {
              "templateName": "checklist"
            }
          }
        ],
        "syncStatus": "IN_CLOUD",
        "executionLog": []
      },
      "customRule": {
        "code": null,
        "executionType": "ON_SUCCESS",
        "description": "Description",
        "owners": null,
        "type": "TWO",
        "enabled": true,
        "objectType": "TEST",
        "syncObjectKPIs": null,
        "inactive": false,
        "responsible": null,
        "id": "5033296D138C45C385AC141E1157B4FE",
        "embedded": true,
        "value": null,
        "variables": [],
        "kafkaEventName": null,
        "lastChanged": 1530091858490,
        "createPerson": null,
        "externalId": null,
        "groups": null,
        "eventType": "UPDATE",
        "branches": null,
        "executionOrder": null,
        "createDateTime": null,
        "cronExpression": null,
        "udfMetaGroups": null,
        "name": "Sample2",
        "location": null,
        "permissionsType": "USER",
        "udfValues": null,
        "conditions": null,
        "actions": [
          {
            "name": "ChecklistInstance",
            "parameters": {
              "templateName": "checklist"
            }
          }
        ],
        "syncStatus": "IN_CLOUD",
        "executionLog": []
      }
    }],
  "pageSize": 1,
  "currentPage": 0,
  "lastPage": 0,
  "totalObjectCount": 1,
  "truncated": false
}

而且我有一个类来映射这个JSON文件。

public class DataResponseDto {
    private List<Map> data;
    private Integer pageSize;
    private Integer currentPage;
    private Integer lastPage;
    private Long totalObjectCount;
    private Boolean truncated;

// getter setter

...
}

现在我使用ObjectMapper将这个JSON解析成一个java类。它检索DataResponseDto时,数据中只有1个映射。它应该是2个数据中的映射。

ClassLoader classLoader = ClassLoader.getSystemClassLoader(); File file = new File(classLoader.getResource("mockDataResponseDto.json").getFile())。

ObjectMapper mapper = new ObjectMapper();
DataResponseDto dataResponseDto = mapper.readValue(file, DataResponseDto.class);
List<RuleDto> rules = dataResponseDto.getData().stream().map(m -> mapper.convertValue(m.get("customRule"), RuleDto.class)).collect(Collectors.toList());

我得到的是 rules.size() = 1,应该是2。

java spring spring-boot spring-mvc objectmapper
1个回答
1
投票

你的问题在于 private List<Map> data;地图不能有重复的键,可以考虑使用其他的东西,如 MultiKeyMap 从apache commons (org.apache.commons.collections.map.MultiKeyMap).


0
投票

你有重复的钥匙 customRule. 试着让... customRules 一个包含规则的数组,然后重新运行代码。

© www.soinside.com 2019 - 2024. All rights reserved.