Restlet, jackson and @JsonTypeInfo

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Restlet, jackson and @JsonTypeInfo

Bluesoft70
Hi,
I'm using POJOs and annotations with json. Using a arraylist in @Get (public ArrayList<T> myGet()), jackson serialize that as {"name":"DITest8","siteOwnerId":255,"siteId":8,"loca....},{...} (using my webbrowser)

On the clientside i therefore get the values in an arraylist with LinkedHashMap instead of my actual object type.

To solve this and add some more info I annotate my serverobject class with @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT) BUT the response is exactly the same.

What i'm I missing?

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3071286
Reply | Threaded
Open this post in threaded view
|

RE: Restlet, jackson and @JsonTypeInfo

Bluesoft70
I was able to convert the LinkedHashMap values to ArrayList<T> using the ObjectMapper in my clientcode:
                        ObjectMapper mapper = jacksonConverter.getObjectMapper();
                        ArrayList<SiteDAO> sites = mapper.convertValue(tmp, new TypeReference<ArrayList<SiteDAO>>() {});

But getting this with a simple annotation i'm not able to figure out yet.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3071291
Reply | Threaded
Open this post in threaded view
|

Re: Restlet, jackson and @JsonTypeInfo

Tim Peierls
One problem is that the generic type formal parameter T doesn't convey enough information for the annotation-based code to reconstruct the correct result type. Try creating a type that embeds the actual value of the type parameter, e.g., SiteDaoArrayList extends ArrayList<SiteDAO> (or SiteDaoArrayList contains List<SiteDAO>), and using that type as the return type of the @Get-annotated method.

--tim


On Tue, Jan 7, 2014 at 7:28 AM, Jon Finanger <[hidden email]> wrote:
I was able to convert the LinkedHashMap values to ArrayList<T> using the ObjectMapper in my clientcode:
                        ObjectMapper mapper = jacksonConverter.getObjectMapper();
                        ArrayList<SiteDAO> sites = mapper.convertValue(tmp, new TypeReference<ArrayList<SiteDAO>>() {});

But getting this with a simple annotation i'm not able to figure out yet.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3071291

Reply | Threaded
Open this post in threaded view
|

Re: Restlet, jackson and @JsonTypeInfo

Thierry Boileau-4
Hello,

I think that the following will also work:
@Get
public ArrayList<SiteDAO> myGet() {
...
}

Best regards,
Thierry Boileau


2014/1/7 Tim Peierls <[hidden email]>
One problem is that the generic type formal parameter T doesn't convey enough information for the annotation-based code to reconstruct the correct result type. Try creating a type that embeds the actual value of the type parameter, e.g., SiteDaoArrayList extends ArrayList<SiteDAO> (or SiteDaoArrayList contains List<SiteDAO>), and using that type as the return type of the @Get-annotated method.

--tim


On Tue, Jan 7, 2014 at 7:28 AM, Jon Finanger <[hidden email]> wrote:
I was able to convert the LinkedHashMap values to ArrayList<T> using the ObjectMapper in my clientcode:
                        ObjectMapper mapper = jacksonConverter.getObjectMapper();
                        ArrayList<SiteDAO> sites = mapper.convertValue(tmp, new TypeReference<ArrayList<SiteDAO>>() {});

But getting this with a simple annotation i'm not able to figure out yet.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3071291


Reply | Threaded
Open this post in threaded view
|

RE: Re: Restlet, jackson and @JsonTypeInfo

Bluesoft70
I'm trying to figure out how to get the android client to automatically convert the representation into the correct Objecttype without using mapper.convertValue(...)

When looking at the representation sent from the server i see that the type information is not present. [{"name":"site1","siteOwnerId":1,"siteId":1,"location":"vega1","lastOnline":null,"port":6194,"bindAddress":"","alarmFlag":false,"onlineStatus":true}...

so no wonder it cant auto-convert. I've added @JsonTypeInfo to the remote interface and @Get("json") on the method, but see no effect of that.

When choosing xml as the representation the typeinfo is present.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3072127
Reply | Threaded
Open this post in threaded view
|

Re: Re: Restlet, jackson and @JsonTypeInfo

Tim Peierls
What arguments are you passing to @JsonTypeInfo?


On Mon, Jan 27, 2014 at 6:57 AM, Jon Finanger <[hidden email]> wrote:
I'm trying to figure out how to get the android client to automatically convert the representation into the correct Objecttype without using mapper.convertValue(...)

When looking at the representation sent from the server i see that the type information is not present. [{"name":"site1","siteOwnerId":1,"siteId":1,"location":"vega1","lastOnline":null,"port":6194,"bindAddress":"","alarmFlag":false,"onlineStatus":true}...

so no wonder it cant auto-convert. I've added @JsonTypeInfo to the remote interface and @Get("json") on the method, but see no effect of that.

When choosing xml as the representation the typeinfo is present.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3072127