<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Building RESTful web services using Jersey</title>
	<atom:link href="http://aruld.info/building-restful-web-services-using-jersey/feed/" rel="self" type="application/rss+xml" />
	<link>http://aruld.info/building-restful-web-services-using-jersey/</link>
	<description>Technology++</description>
	<lastBuildDate>Fri, 28 Oct 2011 18:10:18 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: admin</title>
		<link>http://aruld.info/building-restful-web-services-using-jersey/#comment-976</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Sun, 06 Jul 2008 04:00:13 +0000</pubDate>
		<guid isPermaLink="false">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(&quot;/movies&quot;)
public class MovieResource {

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = &quot;movieDetails&quot;, propOrder = {
        &quot;title&quot;,
        &quot;genres&quot;,
        &quot;directedBy&quot;
    })
    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({&quot;application/xml&quot;, &quot;application/json&quot;})
    public Response getTopBoxOfficeMovieThisWeek() {
        MovieDetails pojo = new MovieDetails();
        pojo.setTitle(&quot;WALL-E&quot;);
        pojo.setDirectedBy(&quot;Andrew Stanton&quot;);
        pojo.setRank(1);
        return Response.ok(pojo).build();
    }

    public static void assertEquals(Object o1, Object o2) {
        if (!o1.equals(o2)) {
            System.out.println(o1 + &quot; != &quot; + o2);
            throw new RuntimeException();
        }
    }

    public static void client() throws Exception {
        Client client = Client.create();
        WebResource resource = client.resource(&quot;http://localhost:9999/movies&quot;);
        MovieDetails pojo = resource.accept(&quot;application/xml&quot;).get(MovieDetails.class);
        assertEquals(&quot;WALL-E&quot;, pojo.getTitle());
        assertEquals(1, pojo.getRank());
        System.out.println(&quot;XML  Response :&quot; + resource.accept(&quot;application/xml&quot;).get(String.class));
        System.out.println(&quot;JSON Response :&quot; + resource.accept(&quot;application/json&quot;).get(String.class));
    }

    public static void main(String[] args) throws Exception {
        HttpServer s = HttpServerFactory.create(&quot;http://localhost:9999/&quot;);
        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(&#8220;/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({&#8220;application/xml&#8221;, &#8220;application/json&#8221;})<br />
    public Response getTopBoxOfficeMovieThisWeek() {<br />
        MovieDetails pojo = new MovieDetails();<br />
        pojo.setTitle(&#8220;WALL-E&#8221;);<br />
        pojo.setDirectedBy(&#8220;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(&#8220;http://localhost:9999/movies&#8221;);<br />
        MovieDetails pojo = resource.accept(&#8220;application/xml&#8221;).get(MovieDetails.class);<br />
        assertEquals(&#8220;WALL-E&#8221;, pojo.getTitle());<br />
        assertEquals(1, pojo.getRank());<br />
        System.out.println(&#8220;XML  Response :&#8221; + resource.accept(&#8220;application/xml&#8221;).get(String.class));<br />
        System.out.println(&#8220;JSON Response :&#8221; + resource.accept(&#8220;application/json&#8221;).get(String.class));<br />
    }</p>
<p>    public static void main(String[] args) throws Exception {<br />
        HttpServer s = HttpServerFactory.create(&#8220;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>
		<dc:creator>Steve</dc:creator>
		<pubDate>Mon, 30 Jun 2008 01:52:33 +0000</pubDate>
		<guid isPermaLink="false">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>
		<dc:creator>&#187; Jersey Client API - made for each other Blogging at the speed of thought: Life, Technology and More</dc:creator>
		<pubDate>Wed, 28 May 2008 02:43:26 +0000</pubDate>
		<guid isPermaLink="false">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>[...] 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 [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

