Sandesha J Sandesha J -4 years ago 162
Java Question

Jackson json to java object mapping

If I pass a JSON like

{
"entity":{
"name":"xyz",
"age":21
}
}

Then , is it compulsory that my POJO should have both "name" and "age" instance variables. I mean, if I have only "name" instance field, will there be any exception while mapping?

Answer Source

is it compulsory that my POJO should have both "name" and "age" instance variables

If you mean "have" as in the same names exactly, then no. You can use annotations to rename the values.

If you instead mean the class definition "contains" those values, then also it shouldn't need them. That depends on how you configure ObjectMapper, though. For example, there is an AnyGetter annotation that will allow you to collect "extra" JSON values. Therefore, it makes sense that mandatory fields are not required.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download