jano
jano

Reputation: 1

nested list with RestTemplate exchange

java.lang.IllegalArgumentException: Cannot deserialize value of type `.dto.Person` from Array value (token `JsonToken.START_ARRAY`)
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: java.util.HashMap[])

JSON

{
  "scully": [
    {
      "beginDate": "2017-10-14",
      "endDate": "2017-10-18",
      "absences": [
        {
          "name": "Annual Leave",
          "number": "1_1",
          "type": "AL",
          "beginDate": "2017-10-14",
          "endDate": "2017-10-17",
          "status": "Checked, OK",
          "workingDays": 2.5,
          "isBeginDateHalfDay": false,
          "isEndDateHalfDay": true
        },
        {
          "name": "Annual Leave",
          "number": "1_2",
          "type": "AL",
          "beginDate": "2017-10-18",
          "endDate": "2017-10-18",
          "status": "Checked, Rejected",
          "workingDays": 0.5,
          "isBeginDateHalfDay": true,
          "isEndDateHalfDay": false
        }
      ]
    }
  ]
}
`@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {
    @JsonProperty("BeginDate")
    public String beginDate;
    public String endDate;
    @JsonProperty("Absences")
    public List<Absence> absences;
}

`@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Absence {
    public String number;
    public String beginDate;
    public boolean isBeginDateHalfDay;

    public String type;

    public String name;
    public String endDate;
    public boolean isEndDateHalfDay;
    public boolean isAfternoonHalfDay;
    public String statusIdentifier;
    public double workingDays;
    public boolean isForenoonHalfDay;
    public String status;
}``
ResponseEntity<HashMap> response = restTemplate.exchange(
                uriBuilder.toUriString(), HttpMethod.GET, httpEntity, new ParameterizedTypeReference<HashMap>() {
                });

        HashMap<String, Person> objects = response.getBody();
        // mapping der ResponseEntity zu Person Objekt...
        ObjectMapper mapper = new ObjectMapper();

        mapper.convertValue(objects, new TypeReference<HashMap<String, Person>>() {
        });
        return objects;

Upvotes: 0

Views: 130

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40078

Use TypeReference<HashMap<String, List<Person>>>() since scully key in json is having List of Person objects

mapper.convertValue(objects, new TypeReference<HashMap<String, List<Person>>>() {
});

and make sure the property names in @JsonPropert annotations are matching with json

@JsonProperty("beginDate")
public String beginDate;

@JsonProperty("absences")
public List<Absence> absences;

Upvotes: 0

Related Questions