You can consider my API as a proxy for connecting to different APIs providing similar kinda data represented in somewhat different way than each other.
On Sunday, September 10, 2017 at 10:00:21 PM UTC+5:30, Tatu Saloranta wrote: > > On Sat, Sep 9, 2017 at 9:58 PM, chetan choulwar <[email protected] > <javascript:>> wrote: > > That @JsonAlias worked fine. But my problem is with nested elements, how > can > > I specify nested json element into alias? > > You can't. If your structures differ beond just naming you need to do > something else. > Databinding is meant to match incoming document structure to a Java > object graph and vice versa. > It is not designed to allow arbitrary transformation, or unification > of differing structures into single model. > > It is possible to unify differing structures, of course, but that is > true transformation and something explicitly > out of scope for Jackson. You can use `JsonNode` as structure modify, > for input and output structures, and it is possible > to change structure any way you want. But you have to write that > transformation yourself. > Or you can use separate Java classes for input(s) and output. > > > And reason for doing so is that I'm writing an API that will send the > common > > response out of all these different APIs I am calling from my API. > > I am still not sure I fully understand your usage, still; this is very > generic explanation. > > But assuming I do understand... if input data you get from a service > differs, you should consider separating handling of your input from > constructing your output. Do not try to use same set of objects if > structures are not same or similar enough. Trying to do that is false > savings and tends to sacrifice clean design for seeming savings. > > This assuming I actually understood what you are trying to do -- > without full explanation of steps from calling another service to > formulating output it is possible to misunderstand intent. > > > Apart from this it'd be great if you can you mail me a link where I can > find > > latest documentation and hands-on for the same. > > > > Thanking you in anticipation..! > > Documentation hub is at: > > https://github.com/FasterXML/jackson-docs > > and main portal > > https://github.com/FasterXML/jackson/ > > has some links. > > -+ Tatu +- > > > > > > > > > On Sunday, September 10, 2017 at 9:21:55 AM UTC+5:30, Tatu Saloranta > wrote: > >> > >> On Fri, Sep 8, 2017 at 5:13 AM, chetan choulwar <[email protected]> > >> wrote: > >> > Thanks a lot for your input. I tried it and it worked as expected. > But > >> > I'm > >> > stuck at a situation where I want to retrieve a value from nested > block, > >> > so > >> > can you help me out? > >> > For example, > >> > By calling REST API "https://xyz.com/resources/resource" gives me { > >> > > >> > "value"={"name":"abc.txt"} > >> > > >> > } > >> > and calling "https://abc.com/resources/resource" gives me > >> > {"title":"idontknow.txt"} > >> > now how can I take name out from the first json response? > >> > > >> > Once again thanks a lot for your answer..!:) > >> > >> I am not sure why my first answer wouldn't work here. All you are > >> doing is specifying that property name in json is an alias that can be > >> used for property in POJO, so you would access it with field name (or > >> getter) you have. > >> > >> But at the same time if these are effectively different objects it is > >> unclear why same Java class should be used anyway -- perhaps they > >> should use different POJOs. > >> > >> -+ Tatu +- > >> > >> > >> > > >> > On Friday, September 8, 2017 at 9:19:49 AM UTC+5:30, Tatu Saloranta > >> > wrote: > >> >> > >> >> On Thu, Sep 7, 2017 at 5:24 AM, chetan choulwar <[email protected]> > > >> >> wrote: > >> >> > Hi All, > >> >> > > >> >> > I'm calling different REST APIs and getting different kinda JSON > >> >> > responses; > >> >> > is there any way to pick particular attribute from different JSON > >> >> > responses > >> >> > and map it to a one common property of defined POJO (Resource for > my > >> >> > API) > >> >> > that can then be sent as a response from the REST API that I've > >> >> > exposed? > >> >> > > >> >> > For Example: > >> >> > By calling REST API "https://xyz.com/resources/resource" gives me > >> >> > {"name":"abc.txt"} > >> >> > and calling "https://abc.com/resources/resource" gives me > >> >> > {"title":"idontknow.txt"} > >> >> > > >> >> > And I have one resource class defined for my APIs to return i.e. > >> >> > MyResource > >> >> > { > >> >> > String fileName; > >> >> > } > >> >> > > >> >> > So is there any way that I can map "name"/"title" to fileName i.e. > >> >> > how > >> >> > can I > >> >> > use jackson to deserialize these jsons to MyResource type? > >> >> > > >> >> > Please let me know if this is valid? and if yes, how? > >> >> > >> >> If you have many different names to use, it may be simpler to just > >> >> bind JSON to `Map` or `JsonNode`, and extract value explicitly. > >> >> > >> >> But if there are just couple of values, you can use `@JsonAlias` > like: > >> >> > >> >> public class POJO { > >> >> @JsonAlias({ "name", "title" }) > >> >> public String fileName; > >> >> } > >> >> > >> >> which would then accept alternate names "name" and "title", but > >> >> serialize as "fileName" (which it also accepts). > >> >> This annotations was added in Jackson 2.9 > >> >> > >> >> -+ Tatu +- > >> > > >> > -- > >> > You received this message because you are subscribed to the Google > >> > Groups > >> > "jackson-dev" group. > >> > To unsubscribe from this group and stop receiving emails from it, > send > >> > an > >> > email to [email protected]. > >> > For more options, visit https://groups.google.com/d/optout. > > > > -- > > You received this message because you are subscribed to the Google > Groups > > "jackson-dev" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to [email protected] <javascript:>. > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "jackson-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
