Max
Max

Reputation: 302

ObjectMapper with nested object

I have JSON response with an array but is not mapping and throwing error, how I can handle JSON like this


{
"total":10,
"count":10,
"start":0,
"items":[
{
"ID":"[email protected]",
"From":{"Relays":null,"Mailbox":"emails","Domain":"gmail.com","Params":""},
"To":[{"Relays":null,"Mailbox":"test","Domain":"gmail.com","Params":""}],
"Content":{"Headers":{"Content-Type":["multipart/mixed; \tboundary=\"----=_Part_27_47303298.1622881481751\""],
"Date":["Sat, 5 Jun 2021 11:24:42 +0300 (EEST)"],
"From":["[email protected]"],
"MIME-Version":["1.0"],

I tried to map like this

 public static class ResponseDto{
        Integer total;
        Integer count;
        Integer start;
        List<ItemDto> items;
//getters,setters
}

public static class ItemDto{
        String id;
}

and I get an error:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ID" (class package.api.utils.ApiMailHogUtil$ItemDto), not marked as ignorable (one known property: "id"])
 at [Source: (StringReader); line: 1, column: 50] (through reference chain: package.api.utils.ApiMailHogUtil$ResponseDto["items"]->java.util.ArrayList[0]->package.api

What I am doing wrong?

Upvotes: 1

Views: 2462

Answers (1)

Most Noble Rabbit
Most Noble Rabbit

Reputation: 2776

As commented, Jackson doesn't find "ID" field so it throws an exception. You can add @JsonProperty("ID") on top of id field in ItemDto:

public static class ItemDto{
        @JsonProperty("ID")
        String id;
        // other fields
}

Upvotes: 2

Related Questions