Re: [Jersey] RESTful WS: How to return something different than String?

From: Oscar <>
Date: Sun, 28 Feb 2010 15:51:20 -0600

Thanks for the tip Kevin. Class generation through XSD works like a
charm because in the project that i'm developing we already have all the
XSD's of the entities that we're going to u


On Feb 25, 2010, at 2:25 PM, Oscar Calderon wrote:

> Ok, thanks to all. I was seeing the samples and i think that
annotations about Craig takls are like @XmlRootElement because i saw
that is used on some objects of jaxb application sample. My only doubt
is about how to return this, because i see in some examples that create
a ResponseBuild object:
> public Response addMessage(String msg) throws URISyntaxException {
> Message m = singleton.addMessage(msg);
> URI msgURI =
> return Response.created(msgURI).build();
> }
> and in other cases only return the object like a normal method:
> @Path("XmlRootElement")
> @GET
> public JAXBXmlRootElement getRootElement() {
> return new JAXBXmlRootElement("xml root element");
> }
> But i think that is about when you use default provider types or
providers that you define right?
You return Response when you need to set a particular HTTP status or
additional HTTP headers. If you are happy with 200 OK and the default
set of headers you can just return the raw object.

- Ocultar texto citado -

> El 2/25/2010 12:16 PM, Kevin Duffey escribió:
>> If you haven't already, take a look at using XSD and generate JAXB
annoated classes from the XSD. The beauty of this is as Craig said, you
get free serialization into objects (when xml or json is sent as party
of the request) and back out to xml/json (depending on the requests
Accept header if it provides application/xml or application/json) with
no work at all on your part. It's very slick, works quite well. Having
the XSD allows for other languages to provide client support by simply
using the XSD to generate the language equivalent classes. I know Craig
did work with Python and Ruby as well as Java for client side using the
same one XSD document.
>> If you're using Jersey on the client side, even better because you
get the same JAXB serialization of client side XSD generated objects
into xml/json (depending on request Content-Type header).
>> I use this exclusively and love it. It makes development a lot
>> --- On Thu, 2/25/10, Craig McClanahan <
<>> wrote:
>> From: Craig McClanahan < <>>
>> Subject: Re: [Jersey] RESTful WS: How to return something different
than String?
>> To: <>
>> Date: Thursday, February 25, 2010, 9:26 AM
>> On Thu, Feb 25, 2010 at 7:00 AM, Oscar Calderon
>> < <>> wrote:
>> > I was reading about this, but it's a little bit confuse for me, but i
>> > understood that first, if i have an object that i want to serialize
>> > (example, a Employee class) i have to build my EmployeeProvider ,
a class
>> > that implements MessageBodyWriter<Employee> then implement the 3
>> > After that, i guess that i have to use that class in my resource
class. I've
>> > seen that the method must return a Response object right? I've
>> > Jersey sample applications, somebody knows if one of that
applications has a
>> > concrete example about how to use MessageBodyWriter (building and
>> > custom providers or using out of the box providers)?
>> >
>> The information you've been given about custom MessageBodyWriter (and
>> MessageBodyReader to process the bodies of incoming requests) is
>> correct. But don't forget that you can get the serialization "for
>> free" if you annotate your Java classes with the appropriate JAXB
>> annotations like @XmlType. For such a Java class, the JAX-RS will
>> provide the appropriate MessageBodyWriter and MessageBodyReader
>> classes automatically.
>> There are examples of this in the "samples" directory as well.
>> Craig McClanahan
>> ------------------------------
>> To unsubscribe, e-mail:
>> For additional commands, e-mail:
> --
> Oscar Calderon
> JAVA Tutorials and How to's? Visit

To unsubscribe, e-mail:
For additional commands, e-mail: