You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Gary Rudolph <ga...@lolay.com> on 2010/08/26 10:20:26 UTC

JAX-RS Response.getEntity and Custom Providers

I have a JAX-RS implementation where I have an interface that returns a Response object. I'm using a custom provider for protocol buffers.

The issue is that on the client I'm registering the providers manually like:
JAXRSClientFactory.create("http://localhost:8888/services/rest", RegistrationResource.class, Arrays.asList(new ProtobufProvider()))

But, yet every time I get the Response the entity is always an HttpInputStream. It never uses the provider to parse to the class. I always have to parse it manually using:
WhereProtos.RegistrationCreatedPayload.parseFrom((InputStream) response.getEntity());

I'd prefer getEntity just returned a WhereProtos.RegistrationCreatedPayload.

Is this expected behavior?

Thanks, Gary

Re: JAX-RS Response.getEntity and Custom Providers

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

On Thu, Aug 26, 2010 at 9:20 AM, Gary Rudolph <ga...@lolay.com> wrote:

> I have a JAX-RS implementation where I have an interface that returns a
> Response object. I'm using a custom provider for protocol buffers.
>
> The issue is that on the client I'm registering the providers manually
> like:
> JAXRSClientFactory.create("http://localhost:8888/services/rest",
> RegistrationResource.class, Arrays.asList(new ProtobufProvider()))
>
> But, yet every time I get the Response the entity is always an
> HttpInputStream. It never uses the provider to parse to the class. I always
> have to parse it manually using:
> WhereProtos.RegistrationCreatedPayload.parseFrom((InputStream)
> response.getEntity());
>
> I'd prefer getEntity just returned a
> WhereProtos.RegistrationCreatedPayload.
>
> Is this expected behavior?
>
>
It is a limitation indeed but the only alternative which can be done is to
introduce some ClientResponse class which will have methods accepting the
type of the expected entity and I'm not sure I'd like to introduce just
another Response class...
I think you can register a MessageBodyReader<Response> in addition to other
ones or instead. Inside its readFrom you can create a Response object using
ResponseBuilder, adding the response headers from the map passed to
readFrom() and setting the entity to the result of
WhereProtos.RegistrationCreatedPayload.parseFrom((InputStream). At least
this will hide the parsing code from the user code...

cheers, Sergey

Thanks, Gary