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

From: Pavel Bucek <Pavel.Bucek_at_Sun.COM>
Date: Thu, 25 Feb 2010 16:10:48 +0100

You can look at jersey-ejb [1] sample. There are two MessageBodyWriter
implementations (MessageWriter and MessageListWriter).

Basically all you need to do is annotate MessageBodyWriter descendant
with @Provider annotation (and let jersey find it) and implements its
methods. @Singleton and @Stateless annotations used in that sample are
EJB related and you can ignore them).



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)?
> El 2/24/2010 10:51 AM, Markus Karg escribió:
>> Hello Oscar,
>> they key to other return types are "MessageBodyWriters": Declare any
>> typed
>> you want, JAX-RS will handle them all. But not out of the box: In
>> short, out
>> of the box you will get Strings (returned als plain text) and JAXB
>> Elements
>> (returned as XML) (plus a bit more stuff like JSON and so on), and
>> OutputStream. If you want to use "any" Object type or "any" kind of MIME
>> entity, you need to register a "conversion" object (i. e.
>> "MessageBodyWriter"). See here:
>> HTH
>> Markus
>>> -----Original Message-----
>>> From: Oscar Calderon []
>>> Sent: Mittwoch, 24. Februar 2010 17:10
>>> To:
>>> Subject: [Jersey] RESTful WS: How to return something different than
>>> String?
>>> Hi to all, i'm a newbie in RESTful web services. I've worked before
>>> with
>>> normal web services and i have a doubt about sending/receiving data
>>> with
>>> service. Somebody knows something like a guide to explain how to return
>>> something different than a String from RESTful web service resource?
>>> For
>>> example return a stream, a list or something like that? That's because
>>> i've developed some simple RESTful web services that returns only a
>>> string, for example a XML string or JSON String, but i haven't found
>>> documentation or something like "How to receive a list as parameter" or
>>> "How to return a stream" or something like that.
>>> Somebody has an idea about some tutorial or documentation to learn
>>> that?
>>> Thanks in advance.
>>> --
>>> Oscar Calderon
>>> JAVA Tutorials and How to's? Visit
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail:
>>> For additional commands, e-mail:
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> For additional commands, e-mail: