That @JsonAlias worked fine. But my problem is with nested elements, how can I specify nested json element into alias? 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. 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..! 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] > <javascript:>> 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] <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.
