users@jersey.java.net

[Jersey] Re: How to get entites (of various type) from ClientResponse

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Fri, 18 Mar 2011 11:37:51 +0100

there is no way how you can get this information.

You can return a wrapper class and check whether information you need is
present or not.

Correct way would be having two different resources (URIs):

GET http://somehost/classA
GET http://somehost/classB

Pavel



On 3/18/11 11:05 AM, Pengfei Di wrote:
> Hallo Pavel,
>
> thanks for the quick reply.
> I mean different classes by different types. The code is like:
> Client c = Client.create();
> WebResource r = c.resoutce("http://somehost/");
> ClientResponse cr =
> r.accept(MediaType.APPLICATION_XML_TYPE).get(ClientResponse.class);
>
> sometimes I need: ClassA objectA= cr.getEntity(ClassA.class);
> but sometimes I need: ClassB objectB= cr.getEntity(ClassB.class);
>
> The question is how can I know which class is included in the client
> response.
>
> Best Regards
> Pengfei
>
>
>
> On 03/18/2011 10:43 AM, Pavel Bucek wrote:
>> Hello Pengfei,
>>
>> it would be better if you shared your code, this way I'm not really
>> sure what are you trying to achieve.
>>
>> What you mean by different types? media types? If so, you can specify
>> this when creating a request:
>>
>> Client c = Client.create();
>> WebResource r = c.resoutce("http://somehost/");
>>
>> ClientResponse cr =
>> r.accept(MediaType.APPLICATION_XML_TYPE).get(ClientResponse.class);
>>
>> And now, you can get entity (for example like string):
>>
>> cr.getEntity(String.class);
>>
>> if you don't know which type you should request here (your resource
>> can return various types as a response to same request) then it is
>> bad design and you should consider some refactoring.
>>
>> Let me know if you have any other questions and please provide more
>> details next time.
>>
>> Regards,
>> Pavel
>>
>>
>> On 3/18/11 9:54 AM, Pengfei Di wrote:
>>> Hello,
>>>
>>> I am trying to use ClientResponse to GET some Entity from Server.
>>> However, the Entity has different types. I am not sure how to solve
>>> the problem.
>>> I am following the user guide, and in chapter 3.5.3 it is written
>>> that the EntityTag can be used. But how should I set the Tag? Just
>>> set the class name inside it?
>>> Or, would it be better to write an own |Unmarshaller|?
>>>
>>> Thanks for any hints
>>>
>>> Pengfei
>>
>