users@jersey.java.net

Re: - consume json & automatic serialization to custom object

From: shan25 <shankar25_at_gmail.com>
Date: Mon, 8 Mar 2010 04:07:49 -0800 (PST)

Thanks for the responses and sorry abt getting back late..

When I removed the @Consumes from @Get method the exception that I mentioned
went away and both the techniques as suggested by Paul (using @Provider or
conversion based on rules ) seems fine except that I will have to do the
deserialization myself either in the provider class or in the String
argument constructor. right ? Wouldn't it be possible to have a support
where the deserialization of the parameter (from json to java custom type)
happens automatically (if i specify using some annotation what is the type
of data-json,xml.. that is excepted in as QueryParam) much like what happens
when I annotate the return object to produce json using @Produces? Infact
that i what i thought would happen when i annotated @GET with
@Consumes("application/json").. let me know if my question isn't making
sense, but i was thinking since the conversion of java to json can happen
the reverse should also be possible and having to write a provider etc where
you have to deserialize and set the data back to the object can be
eliminated.



Paul Sandoz wrote:
>
>
> 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
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
>

-- 
View this message in context: http://n2.nabble.com/consume-json-automatic-serialization-to-custom-object-tp4660991p4694733.html
Sent from the Jersey mailing list archive at Nabble.com.