I have the following json data and wanna create objects that can be used to map to it.
Able to map it fine but it is not feasible.
In this example, I've added itemOne, itemTwo and itemThree.
I end up having to create an Object for each one of these.
In actual scenario, I have over 100 of these and doesn't make sense to be creating 100x classes.
The value for these keys are the same where it is an array of objects.
Is there a way I could reuse a single object to map these?
Cos I need to match these names via a REST call to capture this data in Spring.
Example:
ResponseEntity<Foo> response = restTemplate
.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
Options like annotating JsonProperty would not work for this scenario.
To summarise, I don't want to create 3x classes for itemOne, itemTwo and itemThree.
Looking to map these by reusing a single object.
Or advice if there are other options. Thanks.
Json
{
"mainBody": {
"subData": {
"itemOne": [
{
"header": ""
},
{
"header": ""
}
],
"itemTwo": [
{
"header": ""
},
{
"header": ""
}
],
"itemThree": [
{
"header": ""
},
{
"header": ""
}
]
}
}
The current created objects to map these via ObjectMapper.
@Getter
@Setter
class Foo {
MainBody mainBody;
}
@Getter
@Setter
class ItemOne {
String header;
}
@Getter
@Setter
class ItemThree {
String header;
}
@Getter
@Setter
class ItemTwo {
String header;
}
@Getter
@Setter
class MainBody {
SubData subData;
}
import java.util.List;
@Getter
@Setter
class SubData {
List<ItemOne> itemOne;
List<ItemTwo> itemTwo;
List<ItemThree> itemThree;
}