users@jersey.java.net

[Jersey] Re: HttpRequestContext.getEntity(Class) using Jersey 2.x

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Mon, 8 Jul 2013 13:55:56 +0200

On Jun 23, 2013, at 8:39 AM, cowwoc <cowwoc_at_bbs.darktech.org> wrote:

> Hi,
>
> What is the equivalent of HttpRequestContext.getEntity(Class) for Jersey 2.x? I see ContainerRequestContext.getEntityStream() but how do I go from this InputStream to a POJO?

There's no equivalent. There have been loooooong discussions about this in JAX-RS 2.0 EG. But the introduction of interceptors made us drop this support in favor of compact API.

>
> My use-case is as follows: when a resource attempts to parse the request entity, I'd like to log the entity's String representation so I can reproduce the problem at a later time. In the past I used to:
>
> 1. Cache the entity before it was read by the resource.
> 2. Invoke HttpRequestContext.getEntity(String.class).
>
> I know it's relatively simply to construct a String from an InputStream but tomorrow I might want to construct some other arbitrary POJO from the InputStream, which brings us back to what is the equivalent of HttpRequestContext.getEntity(Class) for Jersey 2.x?

Admittedly, it's not a completely easy task. You need to buffer the entity, convert to string, and then set the buffered input as the new entity stream. See Jersey 2.0 logging filter, that you can use (or mimic it's impl.):
https://jersey.java.net/apidocs/2.0/jersey/org/glassfish/jersey/filter/LoggingFilter.html
https://github.com/jersey/jersey/blob/master/core-common/src/main/java/org/glassfish/jersey/filter/LoggingFilter.java

Marek

>
> Thanks,
> Gili