<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>CodeDependents &#187; Jersey</title>
	<atom:link href="http://codedependents.com/tag/jersey/feed/" rel="self" type="application/rss+xml" />
	<link>http://codedependents.com</link>
	<description>Two Geeks Walk Into A Blog</description>
	<lastBuildDate>Fri, 27 Jan 2012 20:53:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='codedependents.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>CodeDependents &#187; Jersey</title>
		<link>http://codedependents.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://codedependents.com/osd.xml" title="CodeDependents" />
	<atom:link rel='hub' href='http://codedependents.com/?pushpress=hub'/>
		<item>
		<title>Unit Testing Spring Jersey Integration</title>
		<link>http://codedependents.com/2009/04/20/unit-testing-spring-jersey-integration/</link>
		<comments>http://codedependents.com/2009/04/20/unit-testing-spring-jersey-integration/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 02:34:52 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Grizzly]]></category>
		<category><![CDATA[JAX-RS]]></category>
		<category><![CDATA[Jersey]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://blog.bdarfler.com/?p=161</guid>
		<description><![CDATA[Using an embedded Grizzly server to unit test as Spring/Jersey setup.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=161&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been working on a side project which has given me the opportunity to play with an interesting Java technology stack.  After a few weekends of working on the build and moving my way up the stack from database to service layer I was at the point of writing a full integration test.  In the past I&#8217;ve done integration testing with an <a href="http://ibatis.apache.org/">iBatis</a>/<a href="http://www.springsource.org/">Spring</a> stack, which, with the help of <a href="http://www.dbunit.org/">DBUnit</a> is a fairly simple exercise.  However, this project is heavily REST based and the addition of <a href="https://jersey.dev.java.net/">Jersey</a> (JAX-RS) was a serious curveball.</p>
<p>To really test the whole stack an embedded webserver is required.  Looking through the Jersey/Spring unit tests they make use of an exploded WAR using <a href="https://glassfish.dev.java.net/">Glassfish</a>.  Assuming this was the optimal solution I added Glassfish to my ivy.xml config and ran an ivy update.  However, once I realized the overwhelming volume of jars being downloaded I decided it was much too heavy weight of a solution.</p>
<p>My next choice was Jetty.  I have had experience with using Jetty in the past but I quickly ran into issues trying to get it working right with Jersey, Spring, and an exploded WAR configuration.  It was then that I saw the GrizzlyServerFactory referenced in another Jersey unit test.  After some prodding and reading into the source code I ended up with the following:</p>
<p><pre class="brush: java;">
final URI baseUri = UriBuilder.fromUri( &quot;http://localhost/&quot; ).port( 9998 ).build();
final ServletAdapter adapter = new ServletAdapter();
adapter.addInitParameter( &quot;com.sun.jersey.config.property.packages&quot;, &lt;your-package-name&gt; );
adapter.addContextParameter( &quot;contextConfigLocation&quot;,&quot;classpath:applicationContext.xml&quot; );
adapter.addServletListener( &quot;org.springframework.web.context.ContextLoaderListener&quot; );
adapter.setServletInstance( new SpringServlet() );
adapter.setContextPath( baseUri.getPath() );
SelectorThread threadSelector = GrizzlyServerFactory.create( baseUri, adapter );
</pre></p>
<p>We start by making a base Uri for localhost at a high numbered port.  Then we build the Grizzly ServletAdaptor.  The first parameter is the package name where your Jersey enabled are located.  The next two lines are taken directly from the web.xml file that is used for the actual WAR.  The first is the config location for the Spring Context and the second is the ContextLoaderListener servlet.  Finally we add the Jersey SpringServlet and the context path.  With these two objects created we can build a SelectorThread and begin using the server using the Jersey Client api that I talked about in <a href="http://blog.bdarfler.com/2009/04/20/rest-calls-and-json-results-in-java/">my previous post</a>.  When you are done, don&#8217;t forget to call stopEndpoint() on the SelectorThread when you are done.</p>
<p><strong>EDIT</strong>:</p>
<p>I&#8217;ve recently come across one additional helpful tip regarding this setup. If you need to get access to your ApplicationContext, for instance to get access to the spring managed DataSource for database testing, there is a simple but non obvious way of doing this. The simple part is using Spring&#8217;s built in utilities for getting the application from a ServletContext.</p>
<p><pre class="brush: java;">

WebApplicationContextUtils.getWebApplicationContext( adapter.getServletInstance().getServletConfig().getServletContext() );

</pre></p>
<p>However, when I first tried this I got an unexplained null pointer exception. After a long while trying to google around for the answer I gave up and dug into the source code. Turns out you need to add one important property to the ServletAdaptor for this to all work.</p>
<p><pre class="brush: java;">

adapter.setProperty( &quot;load-on-startup&quot;, 1 );

</pre></p>
<p>This will force your adaptor to initialize the servlet immediately and you populate the ServletConfig and ServletContext so you can get the ApplicationContext from them.</p>
<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fcodedependents.com%2F2009%2F04%2F20%2Funit-testing-spring-jersey-integration%2F&amp;title=Unit+Testing+Spring+Jersey%26nbsp%3BIntegration"></a>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=161&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2009/04/20/unit-testing-spring-jersey-integration/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>
	</item>
		<item>
		<title>REST calls and JSON results in Java</title>
		<link>http://codedependents.com/2009/04/20/rest-calls-and-json-results-in-java/</link>
		<comments>http://codedependents.com/2009/04/20/rest-calls-and-json-results-in-java/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 02:01:46 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JAX-RS]]></category>
		<category><![CDATA[Jersey]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://blog.bdarfler.com/?p=158</guid>
		<description><![CDATA[The magic few lines to make REST calls and process a JSON response in Java.  Easy but not obvious.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=158&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently had the need interact with <a href="http://twittervision.com/">Twittervision&#8217;s</a> RESTful api from Java.  As usual, I started googling around for a solution but nothing obviously stood out.  There are a handful of RESTful java frameworks, but these focus on providing a RESTful api and not consuming one.  Finally, after an annoyingly difficult search I found an impressively simple solution based on <a href="https://jersey.dev.java.net/">Jersey</a>.</p>
<p>The point to realize is that Jersey provides both a sever and a client api.  To make use of the client api requires three simple lines:</p>
<p><pre class="brush: java;">
Client client = Client.create();
WebResource webResource = 
client.resource(&quot;http://twittervision.com/user/current_status/bdarfler.json&quot;);
String response = webResource.get(String.class);
</pre><br />
The result is a string of JSON, but now, how to parse it.  After searching around I ended up settling on <a href="http://code.google.com/p/json-simple/">json-simple</a>.  A few simple lines of code and we can get the address for any twitter user.<br />
<pre class="brush: java;">
final JSONObject jsonObj = (JSONObject) JSON_PARSER.parse( response );
if ( jsonObj != null &amp;amp;&amp;amp; jsonObj.containsKey( &quot;location&quot; ) )
{
final JSONObject location = (JSONObject) jsonObj.get( &quot;location&quot; );
return location.get( &quot;address&quot; ).toString();
}
</pre></p>
<p>So, once again, simple stuff but not as obvious as I was hoping it would be.</p>
<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fcodedependents.com%2F2009%2F04%2F20%2Frest-calls-and-json-results-in-java%2F&amp;title=REST+calls+and+JSON+results+in%26nbsp%3BJava"></a>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/158/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=158&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2009/04/20/rest-calls-and-json-results-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>
	</item>
	</channel>
</rss>
