users@jersey.java.net

Re: [Jersey] - consume json & automatic serialization to custom object

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 02 Mar 2010 17:23:51 +0100

On Mar 2, 2010, at 4:58 PM, Jakub Podlesak wrote:

>
> Hi Shankar,
>
> for your scenario, i am afraid, as you do not send the JSON as the
> request body,
> but as a query parameter, this won't work. The exception you are
> getting is probably
> related to the explicit input mime-type (@Consumes annotation),
> which in the case of GET
> request does not make sense.

The exception may also be caused because Jersey does not know what to
do with the SearchQuery type i.e. it does not know how to convert a
String instance to a SearchQuery instance. Without looking at
SearchQuery source i cannot say for sure.

There are certain rules for converting String instances:

   https://jersey.dev.java.net/nonav/documentation/latest/user-guide.html
#d4e226

Jersey also support extension to those described in the above link:

   https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/StringReaderProvider.html

So you can define a StringReaderProvider instance for converting a
String instance to a SearchQuery instance e.g.:

   @Provider
   public class MyProvider implements
StringReaderProvider<SearchQuery> {
     public StringReader<SearchQuery> getStringReader(Class<?> type,
         Type genericType, Annotation[] annotations) {
        if (type == SearchQuery.class) {
          StringReader<SearchQuery> sr = ...
          return sr;
        } else {
          return null
        }
     }
   }

Paul.


> You can workaround this by POSTing the JSON data
> to create a new query resource, return back it's URI location, and
> then GET back
> the query result in a separate GET request without specifying the
> query data.
>
> ~Jakub
>
> On Tue, Mar 02, 2010 at 07:05:21PM +0530, Shankar K wrote:
>> Hi All,
>>
>> I'm trying to see if i can directly consume a json query parameter
>> of a GET
>> request to a custom type object. Can this deserialization (ie. json
>> to java
>> custom object) occur automatically? I'm not able to get it working,
>> provided
>> below the use case, code snippet that I was expecting to work and
>> the error
>> that I get when I deploy. Appreciate your help/suggestions on this!!
>>
>> *Use Case:*
>>
>> my client(browser based) sends a search query as json (which will
>> have
>> page#, page size, filter/sort criteria etc) and on the Rest side,
>> i will
>> have to map it to SearchQuery object which I would use to return to
>> matching
>> objects(as json) back to the client. Following is the code that i
>> have
>> written for the same. But when I deploy it, i get an error which I
>> have also
>> mentioned below.
>>
>> *Code Snippet:
>> *
>> @GET
>> @Consumes("application/json")
>> @Produces("application/json")
>> @Path("/Users")
>>
>> public MinimalUserDTO[]
>> getUserByCriteria(@QueryParam("searchQuery")
>> SearchQuery searchQuery)
>> {
>> //get User Data based on searchQuery
>> //return array of user
>> }
>>
>> *Error:
>> *
>> Method, public java.lang.String getUserByCriteria(MinimalUserDTO[]),
>> annotated with GET of resource, class UserRestService, is not
>> recognized as
>> valid Java method annotated with @HttpMethod.
>>
>> Thanks,
>> Shankar
>
> --
> http://blogs.sun.com/japod
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>