We are building an app that may produce hundreds of unique JSON payload structures from our streamlined object model and we want to avoid adding hundreds of POJOs to our Java codebase (1 per unique payload structure).
We built a way to build, parse and overlay a simple string spec of the payload structure to a POJO and to walk its tree to match any @JsonProperty
fields. Here's an example spec:
String spec = """
firstName
lastName
addresses.line1
addresses.city
children.firstName
children.schedule.bedtime
"""
This would be overlaid on a Person
POJO at runtime and it would traverse and get the fields and arrays specified. Though the Person
, Child
and Address
POJOs have plenty more fields in them, this specific message request should only populate the ones we specified. The JSON output of this should be:
{
"firstName": "Mickey",
"lastName": "Mouse",
"addresses": [
{
"line1": "123 Disneyland Way",
"city": "Anaheim"
},
{
"line2": "345 Disneyworld Drive",
"city": "Orlando"
}
],
"children": [
"firstName": "Junior",
"schedule": [
"bedtime": "20:00"
]
]
}
bear with me here...seems like only 1 Disney character has any children (Ariel)
Currently, we have only tested our Specs and the traversal of POJOs to find where the Spec applies. But we cannot figure out how to wire this into the JSON serialization process.
Everything I've read about Jackson/JSON/Spring serializers and deserializers seem to only take 1 input - the POJO. Is there a way to use custom serializers that use 2 inputs (POJO + Spec), where Spec is identified at runtime (thus eliminating the need to create a "wrapper" POJO per and/or serializer per Spec)?
To make things even more challenging, we want to simplify our @RestController
methods to simply include our new annotation, @APIMessageSpec(<details go here>)
alongside the @ResponseBody
annotation and then have Spring invoke our custom serialization process, passing in the POJO and details of our @APIMessageSpec
(or possibly for us to subclass @ResponseBody
to parameterize the Spec info in its arguments, so that we don't need 2 annotations)?.
Thanks in advance! Michael