On Sun, Sep 10, 2017 at 12:19 PM, chetan choulwar <
cheta...@gmail.com> wrote:
> Okay..Let me help you with one real time example. Consider for an example
> different tv channels have exposed their APIs to get list of programs
> telecast-ed on them. And now suppose I'm trying to expose one APIs that will
> accept channel name and provide the list of programs for that particular
> channel by calling it from my API and converting the response received in my
> own format (a common one). The kind of data I'm getting from different APIs
> is same but their representation is somewhat different, and what I want to
> do is to convert these responses into a common format defined by my API and
> send it as a response from my API.
> So now that I'm sure that I'm dealing with same kinda data represented
> somewhat different, is it okay to convert it into common format?
Yes, but I don't think you should try to use just one single Java
class for all of that.
It makes sense to use one class for representing output to your users,
but I think that if input structures, names vary,
then POJOs to map them to should be different too. You may reuse some
of those for "similar enough" inputs,
but there are limits to how far this helps.
I mean, you will always have to do some work anyway when adopting new
inputs -- even if just adding annotations.
So question there becomes: what approaches is easiest, most reliable
to maintain?
> Currently I'm doing this by mapping each of the responses to JsonNode only,
> but every time I add a new common API I need to write different logic to
> convert the new response into a common one and that to for different APIs
> I'm calling from my API.
> If any kinda JSON response can be mapped to JsonNode, isn't is possible to
> map these different responses to any particular format? I mean can I write
> my resource object in such a way that I'd be able to map these different
> json responses representing same kinda data to my own resource, in some way,
> instead of doing manually by traversing through json tree?
>
> Hope it is clear to now..Initially I thought it's impossible, but you gave
> me direction and I do believe now there'd be some way that I can bind these
> different resources to common one. May be the way json objects are mapped to
> JsonNode.
>
> Please let me know if this is clear to you, as right now you the only hope I
> can see.!
>
> Thank you Tatu..!
It may make sense to map input you get into `JsonNode` and have
separate code to traverse them;
or to have a set of Input POJOs and code to convert from those to
result/output POJO.
There are pros and cons to each approach.