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

From: Kevin Duffey <>
Date: Thu, 25 Feb 2010 10:16:27 -0800 (PST)

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 easier/faster.

--- On Thu, 2/25/10, Craig McClanahan <> wrote:

From: Craig McClanahan <>
Subject: Re: [Jersey] RESTful WS: How to return something different than String?
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 methods.
> 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 download
> Jersey sample applications, somebody knows if one of that applications has a
> concrete example about how to use MessageBodyWriter (building and using
> 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: