I have problems with serializing objects. I trying to deserialize JSON. It contains lists :
[
{
"id": 1,
"regionName": "Test",
"regionWithType": "Resp Test",
"regionType": "Resp ",
"regionFiasId": "0c089b04-099e-4e0e-955a-6bf1ce525f1a",
"regionCode": "19",
"cities": [
{
"id": 23,
"area": "Test1",
"cityName": "Test1",
"cityType": "г",
"cityFiasId": "6b849aa8-0782-4c26-b7ac-a0f413f306c0",
"cityKladrId": "16031",
"cityWithType": "Test1"
},
{
"id": 24,
"area": null,
"cityName": "Kazn",
"cityType": "г",
"cityFiasId": "93b3df57-4c89-44df-ac42-96f05e9cd3b9",
"cityKladrId": "1600000100000",
"cityWithType": "г Казань"
},
{
"id": 25,
"area": "Test2",
"cityName": "Test2",
"cityType": "пгт",
"cityFiasId": "c111218b-04db-4a5a-af50-aca9318791e0",
"cityKladrId": "1600000100",
"cityWithType": "Test2"
}
]
},
{
"id": 2,
"regionName": "Test2",
"regionWithType": "Test2",
"regionType": "Респ",
"regionFiasId": "6f2cbfd8-692a-4ee4-9b16-067210bde3fc",
"regionCode": "02",
"cities": [
{
"id": 343,
"area": "Test1",
"cityName": "Test1",
"cityType": "г",
"cityFiasId": "6b349aa8-0782-4c26-b7ac-a0f413f306c0",
"cityKladrId": "0202300100000",
"cityWithType": "Test1"
},
{
"id": 344,
"area": null,
"cityName": "Test2",
"cityType": "г",
"cityFiasId": "35b3df57-4c89-44df-ac42-96f05e9cd3b9",
"cityKladrId": "0200000100000",
"cityWithType": "Test2"
},
{
"id": 345,
"area": "Test3",
"cityName": "Test3",
"cityType": "пгт",
"cityFiasId": "c111218b-45db-4a5a-af50-aca9318791e0",
"cityKladrId": "0200400000100",
"cityWithType": "Test3"
}
]
}
]
The error is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `RegionResponse` out of START_ARRAY token
at [Source: (String)"[
{
"id": 1,
"regionName": " ",
"regionWithType": " ",
"regionType": " ",
"regionFiasId": "0c089b04-099e-4e0e-955a-6bf1ce525f1a",
"regionCode": "19",
"cities": [
{
"id": 23,
"area": " 1",
"cityName": " 1",
"cityType": " ",
"cityFiasId": "6b849aa8-0782-4c26-b7ac-a0f413f306c0",
"cityKladrId": "16031",
"cityWithType": " 1"
},
{
"id": 24,
"area": null,
"[truncated 1529 chars]; line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) ~[jackson-databind-2.9.8.jar:2.9.8]
I don't want to change my object mapper because I use it in many other classes. My object mapper code :
@SneakyThrows
private CistomResponse processResponse(Class<? extends CistomResponse> responseClass, String body) {
return objectMapper
.readerFor(responseClass)
.readValue(body);
}
In my response class, I want to serialize to list of objects (Regions) with a list of objects (City) :
@Getter
@FieldDefaults(level = PRIVATE)
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class RegionResponse extends SdpResponse implements Serializable {
List<Region> regions;
@Getter
@JsonIgnoreProperties|ignoreUnknown = true)
public static class Region implements Serializable {
@JsonProperty("regionName")
private String regionName;
@JsonProperty("regionWithType")
private String regionWithType;
@JsonProperty("regionFiasId")
private UUID regionFiasID;
@JsonProperty|"regionCode")
private String regionCode;
@JsonProperty|"cities")
private List<City> cities;
@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
public static class City implements Serializable {
@JsonProperty("cityName")
private String cityName;
@JsonProperty("cityFiasId")
private UUID cityFiasID;
@JsonProperty("cityKladrId"
private String cityKladrID;
}
}
}