users@jersey.java.net

[Jersey] Re: Multiple return type with Jersey and JaxB

From: <stephane.jeanjean_at_orange-ftgroup.com>
Date: Wed, 8 Dec 2010 11:10:26 +0100

Hi Paul,

Thanks for your answer, Your mail was in the junk folder :(

My posted code is similar as your proposal with JaxB Management.

The way with a filter to change the satus code is smart ! I have to think about that.

Stéphane


-----Message d'origine-----
De : Paul Sandoz [mailto:Paul.Sandoz_at_oracle.com]
Envoyé : mardi 7 décembre 2010 10:04
À : users_at_jersey.java.net
Objet : [Jersey] Re: Multiple return type with Jersey and JaxB

Hi Stéphane,

IIUC you are saying that the response status code and/or media type cannot be used to distinguish between two or more forms of XML content?

There is a way you can do it, but it is slightly complicated due to the nature of integrating JAXB.

First, you need to ensure that the media type returned is an XML compatible media type.

Second, you need to register a ContextResolver<JAXBContext> component for the relevant JAXB classes.

Third, you need to ensure that the return type you use is Object.class

For example,

public class MyContextResolver implements ContextResolver<JAXBContext> { ... }


ClientConfig cc = new DefaultClientConfig(MyContextResolver.class);
Client c = Client.create(cc);

WebResource r = ...
Object o = r.get(Object.class);
if (o instanceof BusinessObject) {
} else if (o instanceof ErrorObject) {
} else {
   // unrecognized type
}


Another way you could do this is to write a client filter to buffer the response and peek into the XML message using say StAX to look at the root element and from that modify the status code.

Paul.

On Dec 6, 2010, at 6:11 PM, stephane.jeanjean_at_orange-ftgroup.com wrote:

> Hi,
>
> I'm using Jersey as Client API to call REST Services. I'm using JaxB
> entities to get the responses.
> The services return a different entity if there is an error. So I have
> to 2 JaxB classes with an @XmlRootElement : one for my business class
> and one for the error. But I don't know how to use 2 classes with
> Jersey Client API :(
>
> Do you have an idea to manage this kind of REST call, please ?
>
> I found this topic which is exactly my issue :
> http://stackoverflow.com/questions/2025997/multiple-return-type-in-jer
> sey-client-request But I can't change the REST service as proposed in
> the answer, it's not mine.
>
> Thanks,
>
> Stéphane