users@jersey.java.net

Re: [Jersey] Re[Jersey] ading entity body

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 23 Oct 2008 08:57:00 +0200

On Oct 23, 2008, at 1:39 AM, Gili wrote:

>
> So... hmm... this feels like a dumb question, but how am I supposed
> to read
> the entity body? I know you use MessageBodyReader/Writer to read/
> write Java
> classes into the body but I don't think this is what I'm looking for.
>
> I want to read some XML from the body of a HTTP POST and based on its
> content I will either create a new database entry or update an
> existing
> entry. The xml doesn't necessarily have to provide all fields (i.e. if
> you're renaming an existing database entry).
>

What XML API do you want to use?

You want to use POST for partial update?


> Am I supposed to use @Context HttpServerRequest and read the
> InputStream
> that way?
>

You can just use InputStream directly.

    @POST void post(InputStream in) { ... }

Or you can use JAXB, or DOM:

    @POST void post(javax.xml.transform.dom.DOMSource ds) { ... }


> Also, how is MessageBodyReader supposed to know what class type is
> on the
> stream before actually consuming that stream?

MessageBodyReader instances are associated with a Java type and media
type. They are responsible for producing an instance of the Java type
from a sequence of bytes. It is up to the application developer to
declare the Java type as a parameter of the resource method. When
required Jersey will select the most appropriate reader based on the
Java type and media type.


> Does JAX-RS write something
> proprietary onto the HTTP headers or body to this end or is the
> Provider in
> full control of what's getting written?

No. The application developer is in control of the Java type and media
type, which results in the selection of the most appropriate
MessageBodyReader.


> Doesn't this mechanism assume that
> servers and clients will share the same MessageBodyReader/Writer
> implementation?

No.


> Is there such a thing as a JAX-RS client (I was under the
> impression JAX-RS only defines server-end stuff)?
>

JAX-RS is only server-side. Jersey defines a client-side, that reuses
message body readers/writers:

https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0/api/jersey/com/sun/jersey/api/client/package-summary.html

Paul.