I'm building a SpringBoot Client (leveraging WebClient for the actual call) that will be retrieving data from a Vendor's REST API (so we have no control over the response structure). Regardless of the endpoint that is called, all responses are wrapped in either
{
"data": {
"attributes": {
// DESIRED FIELDS
}
}
}
or
{
"data": [
{
"attributes": {
// DESIRED FIELDS
}
},
{
"attributes": {
// SAME DESIRED FIELDS WITH DIFFERENT VALUES
}
},
...
]
}
The only way data + attributes would ever be used is to access the data wrapped within them, so I'm looking for an "invisible hand" that I could setup to automagically extract out and return only the inner POJO(s), preferably during the de-serialization process.
There are ~30 POJOs (which can be combined into any hierarchy/relationship structure as required) that have been created to represent the different responses we can get back from the vendor (packaged into an in-house jar for reuse), so writing a custom deserializer/builder/unpacker/etc for each one isn't really an option, and I'm hitting a wall trying to make a generic deserializer or decoder.
As we have no control/influence over the way the Vendor returns the data, leveraging Jackson's Polymorphic deserialization isn't an option here AFAIK, since there's no way to embed identity fields into the serialized data, nor are there any unique, native fields that could serve to reliably identify any given root/parent object in their JSON (the vendor loves to reuse generic fields, like id and name).
Given this, is there an approach that could allow for behind-the-scenes extraction? Even something like intercepting any JSON de-serialization attempt, looking for the data (both array + object permutations) + attribute fields, then manually manipulating the JSON into the desired structure would work, but I can't find any WebClient or Jackson documentation that suggests the existence of a, i guess "detour", de-serializer/decoder that would allow the chain to continue as normal after restructuring.
Worst comes to worst, I can create a generic wrapper class with helper methods to extract the underlying POJOs post-deserialization, and that can be used in combination with WebClient's chained calls, but that doesn't reduce the bloat/boilerplate all that much.