users@jersey.java.net

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

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Thu, 25 Feb 2010 14:34:09 -0500

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:
>
> @POST
> public Response addMessage(String msg) throws URISyntaxException {
> Message m = singleton.addMessage(msg);
>
> URI msgURI = ui.getRequestUriBuilder().path(Integer.toString(m.getUniqueId())).build();
>
> 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.

Marc.

>
>
>
> 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 easier/faster.
>>
>>
>> --- On Thu, 2/25/10, Craig McClanahan <craigmcc_at_gmail.com> wrote:
>>
>> From: Craig McClanahan <craigmcc_at_gmail.com>
>> Subject: Re: [Jersey] RESTful WS: How to return something different than String?
>> To: users_at_jersey.dev.java.net
>> Date: Thursday, February 25, 2010, 9:26 AM
>>
>> On Thu, Feb 25, 2010 at 7:00 AM, Oscar Calderon
>> <oscar.kalderon_at_gmail.com> 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: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
> --
> Oscar Calderon
>
> JAVA Tutorials and How to's? Visit
> http://www.javahowto.net/