ObjectMapper with nested object

75
June 06, 2021, at 10:00 AM

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":"Hyd4VVGtCNpyLh7JEOL3X-b92yollu9nb3KbweWxNaI=@mailhog.example",
"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":["test@gmail.com"],
"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?

Answer 1

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
}
READ ALSO
this.app.service(&hellip;).create(&hellip;) is not a function

this.app.service(…).create(…) is not a function

I tried to make api request twice and the first one work, but the second fails with below error

58
mglearn.plots.plot_2d_separator (eps=0.5) , what is that,&ldquo;eps&rdquo;?

mglearn.plots.plot_2d_separator (eps=0.5) , what is that,“eps”?

Im' learning Python from the book,"introduction to Machine learning with Python"

111
ASP.NET JavaScript onClick function

ASP.NET JavaScript onClick function

I have an indexcshtml page, in it there is a button:

17
Laravel whereNotIn not working as subquery

Laravel whereNotIn not working as subquery

Hello wonderful people of SO!

75