Search code examples
javaspringspring-bootspring-mvcobjectmapper

ObjectMapper mapping not reading data properly for List<Map>


I have a 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
}

And I have a class to map this JSON file.

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

// getter setter

...
}

Now I am using ObjectMapper to parse this JSON into a java class. It retrieved DataResponseDto with only 1 map in data. It should be 2 maps in data.

ClassLoader classLoader = ClassLoader.getSystemClassLoader(); File file = new File(classLoader.getResource("mock/DataResponseDto.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());

I am getting rules.size() = 1, it should be 2


Solution

  • Your problem is with the private List<Map> data; Map cannot have duplicate keys, consider using something else such as MultiKeyMap from apache commons (org.apache.commons.collections.map.MultiKeyMap).