<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Building RESTful web services using Jersey</title>
	<link>http://aruld.info/building-restful-web-services-using-jersey/</link>
	<description>Life, Technology and More</description>
	<pubDate>Sat, 22 Nov 2008 03:59:14 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: admin</title>
		<link>http://aruld.info/building-restful-web-services-using-jersey/#comment-976</link>
		<author>admin</author>
		<pubDate>Sun, 06 Jul 2008 04:00:13 +0000</pubDate>
		<guid>http://aruld.info/building-restful-web-services-using-jersey/#comment-976</guid>
		<description>Thanks Steve.

You can start with a pojo too. The response is tied to the media type produced by your resource. If you want to generate application/xml or application/json response types, your pojo needs to be annotated with JAXB annotations as shown in the below code.

Let me know if this helps.

-Arul

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@Path("/movies")
public class MovieResource {

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "movieDetails", propOrder = {
        "title",
        "genres",
        "directedBy"
    })
    public static class MovieDetails {

        @XmlElement(required = true)
        private String title;
        @XmlElement(required = true)
        private String genres;
        @XmlElement(required = true)
        private String directedBy;
        @XmlAttribute
        private Integer rank;

        public String getDirectedBy() {
            return directedBy;
        }

        public void setDirectedBy(String directedBy) {
            this.directedBy = directedBy;
        }

        public String getGenres() {
            return genres;
        }

        public void setGenres(String genres) {
            this.genres = genres;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public Integer getRank() {
            return rank;
        }

        public void setRank(Integer rank) {
            this.rank = rank;
        }
    }

    @GET
    @ProduceMime({"application/xml", "application/json"})
    public Response getTopBoxOfficeMovieThisWeek() {
        MovieDetails pojo = new MovieDetails();
        pojo.setTitle("WALL-E");
        pojo.setDirectedBy("Andrew Stanton");
        pojo.setRank(1);
        return Response.ok(pojo).build();
    }

    public static void assertEquals(Object o1, Object o2) {
        if (!o1.equals(o2)) {
            System.out.println(o1 + " != " + o2);
            throw new RuntimeException();
        }
    }

    public static void client() throws Exception {
        Client client = Client.create();
        WebResource resource = client.resource("http://localhost:9999/movies");
        MovieDetails pojo = resource.accept("application/xml").get(MovieDetails.class);
        assertEquals("WALL-E", pojo.getTitle());
        assertEquals(1, pojo.getRank());
        System.out.println("XML  Response :" + resource.accept("application/xml").get(String.class));
        System.out.println("JSON Response :" + resource.accept("application/json").get(String.class));
    }

    public static void main(String[] args) throws Exception {
        HttpServer s = HttpServerFactory.create("http://localhost:9999/");
        s.start();

        try {
            client();
        } finally {
            s.stop(0);
        }
    }
}</description>
		<content:encoded><![CDATA[<p>Thanks Steve.</p>
<p>You can start with a pojo too. The response is tied to the media type produced by your resource. If you want to generate application/xml or application/json response types, your pojo needs to be annotated with JAXB annotations as shown in the below code.</p>
<p>Let me know if this helps.</p>
<p>-Arul</p>
<p>import com.sun.jersey.api.client.Client;<br />
import com.sun.jersey.api.client.WebResource;<br />
import com.sun.jersey.api.container.httpserver.HttpServerFactory;<br />
import com.sun.net.httpserver.HttpServer;<br />
import javax.ws.rs.GET;<br />
import javax.ws.rs.Path;<br />
import javax.ws.rs.ProduceMime;<br />
import javax.ws.rs.core.Response;<br />
import javax.xml.bind.annotation.XmlAccessType;<br />
import javax.xml.bind.annotation.XmlAccessorType;<br />
import javax.xml.bind.annotation.XmlAttribute;<br />
import javax.xml.bind.annotation.XmlElement;<br />
import javax.xml.bind.annotation.XmlRootElement;<br />
import javax.xml.bind.annotation.XmlType;</p>
<p>@Path(&#8221;/movies&#8221;)<br />
public class MovieResource {</p>
<p>    @XmlRootElement<br />
    @XmlAccessorType(XmlAccessType.FIELD)<br />
    @XmlType(name = &#8220;movieDetails&#8221;, propOrder = {<br />
        &#8220;title&#8221;,<br />
        &#8220;genres&#8221;,<br />
        &#8220;directedBy&#8221;<br />
    })<br />
    public static class MovieDetails {</p>
<p>        @XmlElement(required = true)<br />
        private String title;<br />
        @XmlElement(required = true)<br />
        private String genres;<br />
        @XmlElement(required = true)<br />
        private String directedBy;<br />
        @XmlAttribute<br />
        private Integer rank;</p>
<p>        public String getDirectedBy() {<br />
            return directedBy;<br />
        }</p>
<p>        public void setDirectedBy(String directedBy) {<br />
            this.directedBy = directedBy;<br />
        }</p>
<p>        public String getGenres() {<br />
            return genres;<br />
        }</p>
<p>        public void setGenres(String genres) {<br />
            this.genres = genres;<br />
        }</p>
<p>        public String getTitle() {<br />
            return title;<br />
        }</p>
<p>        public void setTitle(String title) {<br />
            this.title = title;<br />
        }</p>
<p>        public Integer getRank() {<br />
            return rank;<br />
        }</p>
<p>        public void setRank(Integer rank) {<br />
            this.rank = rank;<br />
        }<br />
    }</p>
<p>    @GET<br />
    @ProduceMime({&#8221;application/xml&#8221;, &#8220;application/json&#8221;})<br />
    public Response getTopBoxOfficeMovieThisWeek() {<br />
        MovieDetails pojo = new MovieDetails();<br />
        pojo.setTitle(&#8221;WALL-E&#8221;);<br />
        pojo.setDirectedBy(&#8221;Andrew Stanton&#8221;);<br />
        pojo.setRank(1);<br />
        return Response.ok(pojo).build();<br />
    }</p>
<p>    public static void assertEquals(Object o1, Object o2) {<br />
        if (!o1.equals(o2)) {<br />
            System.out.println(o1 + &#8221; != &#8221; + o2);<br />
            throw new RuntimeException();<br />
        }<br />
    }</p>
<p>    public static void client() throws Exception {<br />
        Client client = Client.create();<br />
        WebResource resource = client.resource(&#8221;http://localhost:9999/movies&#8221;);<br />
        MovieDetails pojo = resource.accept(&#8221;application/xml&#8221;).get(MovieDetails.class);<br />
        assertEquals(&#8221;WALL-E&#8221;, pojo.getTitle());<br />
        assertEquals(1, pojo.getRank());<br />
        System.out.println(&#8221;XML  Response :&#8221; + resource.accept(&#8221;application/xml&#8221;).get(String.class));<br />
        System.out.println(&#8221;JSON Response :&#8221; + resource.accept(&#8221;application/json&#8221;).get(String.class));<br />
    }</p>
<p>    public static void main(String[] args) throws Exception {<br />
        HttpServer s = HttpServerFactory.create(&#8221;http://localhost:9999/&#8221;);<br />
        s.start();</p>
<p>        try {<br />
            client();<br />
        } finally {<br />
            s.stop(0);<br />
        }<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://aruld.info/building-restful-web-services-using-jersey/#comment-739</link>
		<author>Steve</author>
		<pubDate>Mon, 30 Jun 2008 01:52:33 +0000</pubDate>
		<guid>http://aruld.info/building-restful-web-services-using-jersey/#comment-739</guid>
		<description>Great article!! Just curious as to why you started with the schema and generated your response object? Is there a way to also create the response with just regular POJOs?

Thanks,
Steve</description>
		<content:encoded><![CDATA[<p>Great article!! Just curious as to why you started with the schema and generated your response object? Is there a way to also create the response with just regular POJOs?</p>
<p>Thanks,<br />
Steve</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: &#187; Jersey Client API - made for each other Blogging at the speed of thought: Life, Technology and More</title>
		<link>http://aruld.info/building-restful-web-services-using-jersey/#comment-603</link>
		<author>&#187; Jersey Client API - made for each other Blogging at the speed of thought: Life, Technology and More</author>
		<pubDate>Wed, 28 May 2008 02:43:26 +0000</pubDate>
		<guid>http://aruld.info/building-restful-web-services-using-jersey/#comment-603</guid>
		<description>[...] Posted in May 27th, 2008  by admin in Web Frameworks, JEE, Java, Design Patterns In my earlier blog entry on Jersey, I used HTTPClient API and curl command line utility as the clients. I had not mentioned [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] Posted in May 27th, 2008  by admin in Web Frameworks, JEE, Java, Design Patterns In my earlier blog entry on Jersey, I used HTTPClient API and curl command line utility as the clients. I had not mentioned [&#8230;]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
