users@jersey.java.net

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

From: Pengfei Di <pengfei.di_at_match2blue.com>
Date: Fri, 18 Mar 2011 11:05:20 +0100

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
>