users@jersey.java.net

Re: [Jersey] Sending xml to Jersey

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 12 Feb 2009 10:27:54 +0100

On Feb 12, 2009, at 1:04 AM, paradisonoir wrote:

>
> Hi,
>
> Thanks for the feedback, I was wondering if you can help me out in
> these as
> well:
>
> 1) Does this resource.accept(MediaType.APPLICATION_XML) automatically
> convert/transform our request to XML?
>

No. You need to provide a correct Java type is capable of being
unmarshalled from XML.


> 2) Does builder.entity(myObject) send my already generated JAXB
> object to a
> remote JERSEY request handler?
>

I presume by "JERSEY request handler" you mean the server-side using
Jersey?

 From the client perspective the Jersey client does not care whether
the server-side is written using Jersey or another framework.

If "myObject" is an instance of a JAXB class then the client API will
marshall that object to an XML document and send that document in the
HTTP request. If you are using Jersey on the server side and your
resource method declares the same JAXB class as a parameter of that
resource method then Jersey will unmarshall the XML document in the
HTTP request to an instance of that JAXB class.

The link to the sample i sent previously does what i have described
above:

   http://download.java.net/maven/2/com/sun/jersey/samples/jaxb/1.0.1/jaxb-1.0.1-project.zip


> 3)As my Jersey Request handler is going to do some post action, and
> send me
> an acknowledgment response (e.g., 200), can I get that acknowledgment
> response by using String acknowledgment = builder.post(String.class)
> (assuming my jersey request handler is returning a response)

For the case of:

    String acknowledgment = builder.post(String.class);

if is implied that the status is 200 so you do not need to check it. A
UniformInterfaceException will be thrown if the status is not what is
expected.

If you want to get other status codes you can do:

    ClientResponse r acknowledgment =
builder.post(ClientResponse.class);

Paul.