You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by JasonAB <ja...@acm.org> on 2010/03/16 02:15:59 UTC
JAX-RS proxy client and getEntity()
We're using the JAX-RS proxy clients, and would like to return 201 (created)
from our POSTs. This requires us to return Response from the server, and
insert our payload as the entity. The server seems to serialize this fine,
but the client does not seem to be running the MessageBodyReader as we would
expect.
On further thought, it doesn't seem that the client would ever know how to
deserialize this stream. The method return type for the proxy is Response,
and even though it knows the Content Type, it has no idea which class these
bytes represent.
I see that Jersey has a getEntity(T) method, where you supply the class you
are expecting to Response. Does CXF need this method as well, and is there
any way to get the proxy clients to use the MessageBodyReaders for getEntity
at this time?
jason
--
View this message in context: http://old.nabble.com/JAX-RS-proxy-client-and-getEntity%28%29-tp27912567p27912567.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: JAX-RS proxy client and getEntity()
Posted by JasonAB <ja...@acm.org>.
Sergey Beryozkin-5 wrote:
>
> Hi Jason
>
> Providing a proxy-based RESTful client api may be regarded as a
> controversial step given there's an opinion it does not really differ much
> from a soap-based client api. IMHO, it's more a matter of taste given that
> what happens on the wire after a proxy does the invocation is very
>
Thanks. My main concern is connection management, and not having to have
every app open and close their HTTP connections. I do wonder if the
WebClient is better for my situation than the straight proxy. That'll be a
question for the bigger group.
Thanks for all your work on CXF - it's nice to see an active JAX ecosystem.
jason
--
View this message in context: http://old.nabble.com/JAX-RS-proxy-client-and-getEntity%28%29-tp27912567p27926942.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: JAX-RS proxy client and getEntity()
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Jason
Providing a proxy-based RESTful client api may be regarded as a
controversial step given there's an opinion it does not really differ much
from a soap-based client api. IMHO, it's more a matter of taste given that
what happens on the wire after a proxy does the invocation is very
HTTP-centric and CXF can do a lot to ensure a proxy stays 'operational' as
long as possible...Essentially, every REST resource has a uniform interface
but every proxy has uniquely named methods, but it is only a client view
(ex, proxy.getBook() vs webClient.get(Book.class)).
Proxy based api certainly helps users with trying to invoke against restful
servers, ultimately a user can see if it's too restricting and if yes then
just switch to a web client (in CXF you can do at runtime)
FYI, RestEasy also provides a proxy-based api
cheers, Sergey
On Tue, Mar 16, 2010 at 8:32 PM, JasonAB <ja...@acm.org> wrote:
>
>
>
> Sergey Beryozkin-5 wrote:
> >
> > I believe Jersey uses a ClientResponse class which may have a method like
> > the one you mentioned but you'd need to have a signature returning a
> > ClientResponse for a proxy method to work as you expect.
> >
>
> Thanks very much for the help - that's about what I expected. It would be
> great is CXF could be the leader in RESTful clients, as that seems to be
> lacking across the board.
>
> jason
>
> --
> View this message in context:
> http://old.nabble.com/JAX-RS-proxy-client-and-getEntity%28%29-tp27912567p27924102.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
>
Re: JAX-RS proxy client and getEntity()
Posted by JasonAB <ja...@acm.org>.
Sergey Beryozkin-5 wrote:
>
> I believe Jersey uses a ClientResponse class which may have a method like
> the one you mentioned but you'd need to have a signature returning a
> ClientResponse for a proxy method to work as you expect.
>
Thanks very much for the help - that's about what I expected. It would be
great is CXF could be the leader in RESTful clients, as that seems to be
lacking across the board.
jason
--
View this message in context: http://old.nabble.com/JAX-RS-proxy-client-and-getEntity%28%29-tp27912567p27924102.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: JAX-RS proxy client and getEntity()
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Jason
Given that it is a proxy one can not indicate in a type safe way how to read
an entity stream into some custom class when a proxy method returns
Response. It is just not possible.
I believe Jersey uses a ClientResponse class which may have a method like
the one you mentioned but you'd need to have a signature returning a
ClientResponse for a proxy method to work as you expect.
You have a number of options to deal with this case.
1. Have a method signature returning a specific custom class and, on the
server side, add a simple ResponseHandler filter which sets 201 for POST
responses. On the client side do
Book book = proxy.getBook();
WebClient.client(proxy).getResponse().getStatus();
2. Register a Response MessageBodyReader on the client side (may be CXF
should provide OTB?), which will have a MessageContext injected :
@Context MessageContext mc;
injected and will delegate to this injected providers instance to
deserialize an input stream :
public ResponseMessageBodyReader implements MessageBodyReader<Response> {
public Response readFrom(...Annotation[] anns, MultivaluedMap
headers,..., InputStream is) {
MessageBodyReader entityStreamReader =
mc.getProviders().getMessageBodyReader(CustomClass.class,
CustomClass.class, anns, headers.getFirst("Content-Type"));
Object entity = entityStreamReader.readFrom(CustomClass.class,
CustomClass.class, anns, headers, is);
ResponseBuilder rb =
Response.status(mc.getContextualProperty(Message.RESPONSE_CODE)).entity(entity);
// copy headers using rb.header() method
return rb.build();
}
}
It will actually work fine but you'll need to do a cast at the proxy level :
(Book)proxy.getBook().getEntity()
Perhaps CXF should ship such a reader OTB
3. If neither of the above options works for you then consider using
WebClient instead :
Book b = webClient.get(Book.class);
webClient.getResponse().getStatus();
cheers, Sergey
On Tue, Mar 16, 2010 at 1:15 AM, JasonAB <ja...@acm.org> wrote:
>
>
> We're using the JAX-RS proxy clients, and would like to return 201
> (created)
> from our POSTs. This requires us to return Response from the server, and
> insert our payload as the entity. The server seems to serialize this fine,
> but the client does not seem to be running the MessageBodyReader as we
> would
> expect.
>
> On further thought, it doesn't seem that the client would ever know how to
> deserialize this stream. The method return type for the proxy is Response,
> and even though it knows the Content Type, it has no idea which class these
> bytes represent.
>
> I see that Jersey has a getEntity(T) method, where you supply the class you
> are expecting to Response. Does CXF need this method as well, and is there
> any way to get the proxy clients to use the MessageBodyReaders for
> getEntity
> at this time?
>
> jason
> --
> View this message in context:
> http://old.nabble.com/JAX-RS-proxy-client-and-getEntity%28%29-tp27912567p27912567.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
>