The issue is i'm getting this error.
I have to simulate rest service call because it's being developed now by another team.
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.egencia.service.invoiceaggregator.cache.SaleListDTO out of START_ARRAY token
Here is my jackson mapper bean
Jackson2ObjectMapperBuilder
.json()
.featuresToEnable(DeserializationFeature.UNWRAP_ROOT_VALUE, DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) .serializationInclusion(JsonInclude.Include.NON_NULL)
.serializationInclusion(JsonInclude.Include.NON_EMPTY)
.failOnUnknownProperties(false)
.build();
@JsonRootName("list")
public class SaleListDTO {
private SaleDTO[] list;
public SaleDTO[] getList() {
return list;
}
public void setList(SaleDTO[] list) {
this.list = list;
}
}
Here is the JsON file
{"list": [
{
"id": 111111,
"currency": "EUR",
"country": "ITA",
"name": "Italy",
"code": "IT"
},...
]}
I have tested so many combinations but in vain. please help
Remove @JsonRootName("list").
Here is the working example :
@Getter
@Setter
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class SaleListDTO {
@JsonProperty("list")
private SaleDTO[] list;
}
@Getter
@Setter
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class SaleDTO {
private int id;
private String currency;
private String country;
private String name;
private String code;
}
Test Method:
@Test
public void testConversion() throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper=new ObjectMapper();
SaleListDTO dto=mapper.readValue(new File(PATH), SaleListDTO.class);
System.out.println(dto.toString());
}
Response :
SaleListDTO(list=[SaleDTO(id=111111, currency=EUR, country=ITA, name=Italy, code=IT), SaleDTO(id=22222, currency=IN, country=INDIA, name=CHENNAI, code=IT)])
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments