<?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/"
	>

<channel>
	<title>Better Software and beyond... &#187; Articles</title>
	<atom:link href="http://www.thorsten-kamann.de/category/articles/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thorsten-kamann.de</link>
	<description>About architecture, development, quality and more</description>
	<lastBuildDate>Tue, 25 Oct 2011 20:16:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Writing technical blogs</title>
		<link>http://www.thorsten-kamann.de/2011/10/25/writing-technical-blogs/</link>
		<comments>http://www.thorsten-kamann.de/2011/10/25/writing-technical-blogs/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 20:16:06 +0000</pubDate>
		<dc:creator>thorque</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[egit]]></category>
		<category><![CDATA[Gists]]></category>
		<category><![CDATA[GIT]]></category>

		<guid isPermaLink="false">http://www.thorsten-kamann.de/?p=633</guid>
		<description><![CDATA[<a href="http://www.thorsten-kamann.de/2011/10/25/writing-technical-blogs/" title="Writing technical blogs"></a>If you are an author writing technical blogs you know the problems: the rich text editors doesn&#8217;t support editing source code very well the most editors doesn&#8217;t support tabs (useful for XML) often your formatted sourcecode will be shot But &#8230;<p class="read-more"><a href="http://www.thorsten-kamann.de/2011/10/25/writing-technical-blogs/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.thorsten-kamann.de/2011/10/25/writing-technical-blogs/" title="Writing technical blogs"></a><p>If you are an author writing technical blogs you know the problems:</p>
<ul>
<li>the rich text editors doesn&#8217;t support editing source code very well</li>
<li>the most editors doesn&#8217;t support tabs (useful for XML)</li>
<li>often your formatted sourcecode will be shot</li>
</ul>
<p>But now we are living in the age of social coding. Since GitHub we are knowing that our code can be forked, commented and reviewed by all users in the internet <img src='http://www.thorsten-kamann.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
But GitHub has another nice feature named GIST. A Gist is a repository containing one or more code snippets. This is versioned like your other sourcecode and every Gist is public and can be used as simple code in your blog posts.</p>
<h4>WordPress and Gists</h4>
<p>If your are using WordPress as blogging software you can easily integrate your gists. If you search for Gist plugins you will find some: http://wordpress.org/extend/plugins/search.php?q=gist<br />
I am using this <a title="Embeded GitHub Gist" href="http://wordpress.org/extend/plugins/embed-github-gist/" target="_blank">one</a> and it works fine. For other blogging software you can embedd your gist with a javascript (see below).</p>
<h4>Eclipse and Gists</h4>
<p>The current release of Egit (the Eclipse integration of GIT) offers a GitHub integration. And additionally Gists are supported, too. You can add your Gists with Mylyn and attach and remove files to/from your gist.</p>
<p>IntelliJ 11 has Gist support, too.</p>
<h4>Working with Gist</h4>
<p>There are some steps to do to use gists in your blog:</p>
<ol>
<li>Login into the Gist area of GitHub (you can use your GitHub account)</li>
<li>Create a new Gist</li>
<li>Add one or more files (code snippets)</li>
<li>Embed this in your blog post:<br />
[gist id=12345]<br />
if you have only one file or<br />
[gist id=12345 file=myfile.txt]<br />
if you want to specify a file directly</li>
<li>Thats all!</li>
</ol>
<h4><span class="Apple-style-span" style="line-height: 18px;">Working with Gist without plugin</span></h4>
<p><span class="Apple-style-span" style="line-height: 18px;">You can use your Gists without any plugin, too. It&#8217;s quite simple. Add this snippet to your page:</span></p>
<pre>&lt;script src="https://gist.github.com/12345.js?file=myfile.txt"&gt;&lt;/script&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.thorsten-kamann.de/2011/10/25/writing-technical-blogs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating RESTful webservices with the Spring Framework</title>
		<link>http://www.thorsten-kamann.de/2011/10/15/creating-restful-webservices-with-the-spring-framework/</link>
		<comments>http://www.thorsten-kamann.de/2011/10/15/creating-restful-webservices-with-the-spring-framework/#comments</comments>
		<pubDate>Sat, 15 Oct 2011 21:53:52 +0000</pubDate>
		<dc:creator>thorque</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Webservice]]></category>
		<category><![CDATA[XStream]]></category>

		<guid isPermaLink="false">http://www.thorsten-kamann.de/?p=552</guid>
		<description><![CDATA[<a href="http://www.thorsten-kamann.de/2011/10/15/creating-restful-webservices-with-the-spring-framework/" title="Creating RESTful webservices with the Spring Framework"></a>With the Spring Framework you can easily create RESTful webservices. This is built-in the MVC part of Spring. In this post I show you how to configure this and offer the consumer the response as XML and as JSON. The domain model &#8230;<p class="read-more"><a href="http://www.thorsten-kamann.de/2011/10/15/creating-restful-webservices-with-the-spring-framework/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.thorsten-kamann.de/2011/10/15/creating-restful-webservices-with-the-spring-framework/" title="Creating RESTful webservices with the Spring Framework"></a>
<p>With the Spring Framework you can easily create RESTful webservices. This is built-in the MVC part of Spring. In this post I show you how to configure this and offer the consumer the response as XML and as JSON.</p>
<h4>The domain model</h4>
<p>First we need a domainmodel. In this sample it is quite simple. It consists of 2 classes: Constellation and ConstellationName.</p>
<p style="text-align: center;"><div class="img size-medium wp-image-553 aligncenter" style="width:300px;">
	<a title="Domain Model" href="http://www.thorsten-kamann.de/wp-content/uploads/2011/10/Domainmodel-of-Tucana.png" rel="lightbox"><img src="http://www.thorsten-kamann.de/wp-content/uploads/2011/10/Domainmodel-of-Tucana-300x141.png" alt="" width="300" height="141" /></a>
	<div>Domainmodel</div>
</div>
<p style="text-align: left;">The Constellation describes an astronomical constellation like Orion, Andromeda and so on. The association to ConstellationName offers names of the constellation in different languages.</p>
<h4>The service model</h4>
<p>In this sample the interface used by clients (web, webservice) is a classical service class. This class &#8211; named ConstellationService &#8211; needs a repository class to access the underlying database.</p>
<p style="text-align: center;"><div class="img size-medium wp-image-554 aligncenter" style="width:300px;">
	<a title="Service Model" href="http://www.thorsten-kamann.de/wp-content/uploads/2011/10/Servicemodel-of-Tucana.png" rel="lightbox"><img src="http://www.thorsten-kamann.de/wp-content/uploads/2011/10/Servicemodel-of-Tucana-300x138.png" alt="" width="300" height="138" /></a>
	<div>Servicemodel</div>
</div>
<h4 style="text-align: left;">Wiring domain and service</h4>
<p>With Spring this is quite easy. You need only a little XML file and some Annotations. In the ConstellationService you add the @Service annotation:</p>
<div id="gist-1289371" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nd">@Service</span></div><div class='line' id='LC2'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Service</span><span class="o">{</span></div><div class='line' id='LC3'><span class="o">...</span></div><div class='line' id='LC4'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1289371/f40b2955a76a72aee18bad6466db8bd678a925f8/Service.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1289371#file_service.java" style="float:right;margin-right:10px;color:#666">Service.java</a>
            <a href="https://gist.github.com/1289371">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This annotation is out of the package <em>org.springframework.stereotype.Service</em>. To inject the repository class you can use the @Inject annotation. If you have setup your JPA environment properly you can access the database and get some Constellation instances.</p>
<h4>Creating the REST service</h4>
<p>Now we have a working backend. But we want to have a RESTful webservice so we need another artifact in our sourcecode: the Controller. A controller is the first item of our application. This means if a client requests data the controller gets the request foremost. The controller then is analyzing and validating the request. After this service will be called, the retrieving data prepared and returned.</p>
<p>In Spring you can add some annotations to a controller method to map this method to the specified URL resource:</p>
<div id="gist-1289371" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nd">@RequestMapping</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&quot;/constellations&quot;</span><span class="o">)</span></div><div class='line' id='LC2'><span class="kd">public</span> <span class="n">ModelAndView</span> <span class="nf">getAllConstellations</span><span class="o">()</span> <span class="o">{}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1289371/37442e0e7e14626a0fff7015a6b87e9925c967f1/RequestMapping1.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1289371#file_request_mapping1.java" style="float:right;margin-right:10px;color:#666">RequestMapping1.java</a>
            <a href="https://gist.github.com/1289371">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>With this mapping this controller method responses to the request http://some-host/constellations only. You can map more complex URL resources too. For instance if you have an URL with parameter. In our service we want to offer a method the client can request for one constellation identified by its code. This can be done with the annotation @RequestMapping and @PathVariable:</p>
<div id="gist-1289371" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&quot;/constellation_by_code/{code}&quot;</span><span class="o">)</span></div><div class='line' id='LC2'><span class="kd">public</span> <span class="n">ModelAndView</span> <span class="nf">findConstellationByCode</span><span class="o">(</span><span class="nd">@PathVariable</span> <span class="n">String</span> <span class="n">code</span><span class="o">)</span> <span class="o">{}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1289371/a0058f8b1ad38cf440d4550e2915e6c11e922ab6/RequestMapping2.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1289371#file_request_mapping2.java" style="float:right;margin-right:10px;color:#666">RequestMapping2.java</a>
            <a href="https://gist.github.com/1289371">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>With this mapping the part with the {} will be taken as parameter for the method. You can use more placeholder too. For this you can read the <a title="@RequestMapping in the Spring documentation" href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping" target="_blank">Spring documentation</a>.</p>
<p>Now we have all informations to create the controller class.</p>
<div id="gist-1289371" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nd">@Controller</span></div><div class='line' id='LC2'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ConstellationServiceController</span> <span class="o">{</span></div><div class='line' id='LC3'>&nbsp;</div><div class='line' id='LC4'><span class="nd">@Inject</span></div><div class='line' id='LC5'><span class="kd">private</span> <span class="n">ConstellationService</span> <span class="n">service</span><span class="o">;</span></div><div class='line' id='LC6'>&nbsp;</div><div class='line' id='LC7'><span class="nd">@RequestMapping</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&quot;/constellations&quot;</span><span class="o">)</span></div><div class='line' id='LC8'><span class="kd">public</span> <span class="n">ModelAndView</span> <span class="nf">getAllConstellations</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;<span class="n">List</span><span class="o">&lt;</span><span class="n">Constellation</span><span class="o">&gt;</span> <span class="n">constellations</span> <span class="o">=</span> <span class="n">service</span><span class="o">.</span><span class="na">findAllConstellations</span><span class="o">();</span></div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="n">ModelAndView</span> <span class="n">mav</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ModelAndView</span><span class="o">(</span><span class="s">&quot;xmlView&quot;</span><span class="o">,</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">BindingResult</span><span class="o">.</span><span class="na">MODEL_KEY_PREFIX</span> <span class="o">+</span> <span class="s">&quot;constellations&quot;</span><span class="o">,</span> <span class="n">constellations</span><span class="o">);</span></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="k">return</span> <span class="n">mav</span><span class="o">;</span></div><div class='line' id='LC13'><span class="o">}</span></div><div class='line' id='LC14'>&nbsp;</div><div class='line' id='LC15'><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&quot;/constellation_by_code/{code}&quot;</span><span class="o">)</span></div><div class='line' id='LC16'><span class="kd">public</span> <span class="n">ModelAndView</span> <span class="nf">findConstellationByCode</span><span class="o">(</span><span class="nd">@PathVariable</span> <span class="n">String</span> <span class="n">code</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="n">Constellation</span> <span class="n">constellation</span> <span class="o">=</span> <span class="n">service</span><span class="o">.</span><span class="na">findConstellationByCode</span><span class="o">(</span><span class="n">code</span><span class="o">);</span></div><div class='line' id='LC18'>&nbsp;&nbsp;<span class="k">return</span> <span class="k">new</span> <span class="nf">ModelAndView</span><span class="o">(</span><span class="s">&quot;xmlView&quot;</span><span class="o">,</span> <span class="n">BindingResult</span><span class="o">.</span><span class="na">MODEL_KEY_PREFIX</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">+</span> <span class="s">&quot;constellations&quot;</span><span class="o">,</span> <span class="n">constellation</span><span class="o">);</span></div><div class='line' id='LC20'><span class="o">}</span></div><div class='line' id='LC21'>&nbsp;</div><div class='line' id='LC22'><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&quot;/constellations_by_search/{search}&quot;</span><span class="o">)</span></div><div class='line' id='LC23'><span class="kd">public</span> <span class="n">ModelAndView</span> <span class="n">findConstellationByCodeOrName</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nd">@PathVariable</span> <span class="n">String</span> <span class="n">search</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC25'>&nbsp;&nbsp;<span class="k">return</span> <span class="k">new</span> <span class="nf">ModelAndView</span><span class="o">(</span><span class="s">&quot;xmlView&quot;</span><span class="o">,</span> <span class="n">BindingResult</span><span class="o">.</span><span class="na">MODEL_KEY_PREFIX</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">+</span> <span class="s">&quot;constellations&quot;</span><span class="o">,</span> <span class="n">service</span><span class="o">.</span><span class="na">findAllConstellationByCodeOrName</span><span class="o">(</span><span class="n">search</span><span class="o">));</span></div><div class='line' id='LC27'><span class="o">}</span></div><div class='line' id='LC28'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1289371/6fa57ccb22c6e9411ab35b66c6e63bb88d1406bb/ConstellationServiceController.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1289371#file_constellation_service_controller.java" style="float:right;margin-right:10px;color:#666">ConstellationServiceController.java</a>
            <a href="https://gist.github.com/1289371">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The last step we need to do is to configure the Spring container to get it together. For this we need a little bit XML. In this XML we configure 2 views: one for a XML and one for a JSON response. So you need to add the xml or json extension to the url to control the format of the response.</p>
<div id="gist-1289371" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;bean</span> <span class="na">class=</span><span class="s">&quot;org.springframework.web.servlet.view.BeanNameViewResolver&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC2'>&nbsp;</div><div class='line' id='LC3'><span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;xstreamMarshaller&quot;</span> <span class="na">class=</span><span class="s">&quot;org.springframework.oxm.xstream.XStreamMarshaller&quot;</span></div><div class='line' id='LC4'><span class="na">p:autodetectAnnotations=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC5'>&nbsp;</div><div class='line' id='LC6'><span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;xmlView&quot;</span> <span class="na">class=</span><span class="s">&quot;org.springframework.web.servlet.view.xml.MarshallingView&quot;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">p:contentType=</span><span class="s">&quot;application/xml&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;constructor-arg</span> <span class="na">ref=</span><span class="s">&quot;xstreamMarshaller&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC9'><span class="nt">&lt;/bean&gt;</span></div><div class='line' id='LC10'>&nbsp;</div><div class='line' id='LC11'><span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;jsonView&quot;</span> <span class="na">class=</span><span class="s">&quot;org.springframework.web.servlet.view.json.MappingJacksonJsonView&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC12'>&nbsp;</div><div class='line' id='LC13'><span class="nt">&lt;bean</span> <span class="na">class=</span><span class="s">&quot;org.springframework.web.servlet.view.ContentNegotiatingViewResolver&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;mediaTypes&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;map&gt;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;entry</span> <span class="na">key=</span><span class="s">&quot;json&quot;</span> <span class="na">value=</span><span class="s">&quot;application/json&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;entry</span> <span class="na">key=</span><span class="s">&quot;xml&quot;</span> <span class="na">value=</span><span class="s">&quot;application/xml&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/map&gt;</span></div><div class='line' id='LC19'>&nbsp;&nbsp;<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;defaultViews&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;ref</span> <span class="na">bean=</span><span class="s">&quot;jsonView&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;ref</span> <span class="na">bean=</span><span class="s">&quot;xmlView&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC26'>&nbsp;&nbsp;<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;ignoreAcceptHeader&quot;</span> <span class="na">value=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC27'><span class="nt">&lt;/bean&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1289371/66ec721d1fa99709925728bdeb7ea76f9d97b1da/rpc-context.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1289371#file_rpc_context.xml" style="float:right;margin-right:10px;color:#666">rpc-context.xml</a>
            <a href="https://gist.github.com/1289371">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>First we configure the XMLMarshaller to generate XML out of the Java beans. Then we need 2 Views. One for XML and one for JSON. The most configuration needs the <em>ContentNegotiatingViewResolver</em>. This resolver handles the different response formats. The last property <em>ignoreAcceptHeader</em> you need to get JSON in your browser too. Otherwise always XML will be returned.</p>
<h4>Configuring the web.xml</h4>
<p>You need to activate the Spring servlet and the context listener to get the service running. This is done with a small web.xml.</p>
<div id="gist-1289371" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;web-app&gt;</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nt">&lt;context-param&gt;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;param-name&gt;</span>contextConfigLocation<span class="nt">&lt;/param-name&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;param-value&gt;</span>/WEB-INF/rpc-servlet.xml<span class="nt">&lt;/param-value&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="nt">&lt;/context-param&gt;</span></div><div class='line' id='LC6'>&nbsp;</div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="nt">&lt;listener&gt;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;listener-class&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;org.springframework.web.context.ContextLoaderListener</div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/listener-class&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="nt">&lt;/listener&gt;</span></div><div class='line' id='LC12'>&nbsp;</div><div class='line' id='LC13'>&nbsp;&nbsp;<span class="nt">&lt;servlet&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;servlet-name&gt;</span>rpc<span class="nt">&lt;/servlet-name&gt;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;servlet-class&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="nt">&lt;/servlet-class&gt;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;load-on-startup&gt;</span>1<span class="nt">&lt;/load-on-startup&gt;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="nt">&lt;/servlet&gt;</span></div><div class='line' id='LC18'>&nbsp;</div><div class='line' id='LC19'>&nbsp;&nbsp;<span class="nt">&lt;servlet-mapping&gt;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;servlet-name&gt;</span>rpc<span class="nt">&lt;/servlet-name&gt;</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;url-pattern&gt;</span>/rpc/*<span class="nt">&lt;/url-pattern&gt;</span></div><div class='line' id='LC22'>&nbsp;&nbsp;<span class="nt">&lt;/servlet-mapping&gt;</span></div><div class='line' id='LC23'><span class="nt">&lt;/web-app&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1289371/9ab96c4e6c65efa26201dbec2b92fefa075dccd4/web.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1289371#file_web.xml" style="float:right;margin-right:10px;color:#666">web.xml</a>
            <a href="https://gist.github.com/1289371">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now we have all together and can start the web application. You can access the services with <em>http://localhost:8080/rpc/constellations</em>. As default format XML will be returned. If you want to have JSON change the URL into <em>http://localhost:8080/rpc/constellations.json</em>. Of course you can add .xml to get XML output, too.</p>
<p>If you want to have the data for one constellation you can use the 2nd controller method with <em>http://localhost:8080/rpc/constellation_by_code/and</em>. The last part of the URL is the official code/abbreviation of a constellation. All codes/abbreviations you can get <a title="List of all 88 constellations" href="http://en.wikipedia.org/wiki/List_of_constellations" target="_blank">here</a>. If you want the output as JSON you can add the extension <em>.json</em> too: <em>http://localhost:8080/rpc/constellation_by_code/and.json</em></p>
<h4>Optimize the output</h4>
<p>We have even 2 little problems:</p>
<ol>
<li>The Constellation class defines a byte array containing imagedata of a starcard. If we want to retrieve all constellations the response will be great.</li>
<li>The association will not be resolved. We want to get all names of a constellation with one request to minimize the response-request cycle.</li>
</ol>
<div><span class="Apple-style-span" style="line-height: 18px;">For the marshalling between XML and JAVA code we use <a title="More about XStream" href="http://xstream.codehaus.org/" target="_blank">XStream</a>. With this library you can (de-)serialize XML from Java and vice versa. With XStream you can add an annotation to a class property to avoid the serialization. This annotation is named <em>XStreamOmmitField</em>.</span></div>
<div id="gist-1289371" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Constellation</span><span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="o">...</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nd">@XStreamOmmitField</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="kd">private</span> <span class="kt">byte</span><span class="o">[]</span> <span class="n">starCardData</span><span class="o">;</span></div><div class='line' id='LC5'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1289371/aed60984f7413c64cc36e146989702a41f703672/Constellation.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1289371#file_constellation.java" style="float:right;margin-right:10px;color:#666">Constellation.java</a>
            <a href="https://gist.github.com/1289371">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
The 2nd problem can be solved with the @XStreamImplicit annotation. The collection will be resolved and ebedded in the return data. Additionally you can set the name of the parent element of the collection in the XML data.</p>
<p><div id="gist-1289371" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Constellation</span><span class="o">{</span></div><div class='line' id='LC2'><span class="o">...</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="nd">@XStreamImplicit</span><span class="o">(</span><span class="n">itemFiledName</span> <span class="o">=</span> <span class="s">&quot;names&quot;</span><span class="o">)</span></div><div class='line' id='LC5'><span class="kd">private</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">ConstellationName</span><span class="o">&gt;</span> <span class="n">names</span><span class="o">;</span></div><div class='line' id='LC6'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1289371/519354ad901c69e597a058ea19ffd3a2f695fd2c/Constellation1.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1289371#file_constellation1.java" style="float:right;margin-right:10px;color:#666">Constellation1.java</a>
            <a href="https://gist.github.com/1289371">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The XML result should look like</p>
<div id="gist-1289371" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;constallation&gt;</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nt">&lt;id&gt;</span>4<span class="nt">&lt;/id&gt;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nt">&lt;name&gt;</span>Andromeda<span class="nt">&lt;/name&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nt">&lt;code&gt;</span>and<span class="nt">&lt;/code&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;...</div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="nt">&lt;names&gt;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;id&gt;</span>19<span class="nt">&lt;/id&gt;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;name&gt;</span>Andromeda<span class="nt">&lt;/name&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;langCode&gt;</span>de<span class="nt">&lt;/langCode&gt;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;code&gt;</span>and<span class="nt">&lt;/code&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="nt">&lt;/names&gt;</span></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="nt">&lt;names&gt;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;id&gt;</span>20<span class="nt">&lt;/id&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;name&gt;</span>Andromeda<span class="nt">&lt;/name&gt;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;langCode&gt;</span>en<span class="nt">&lt;/langCode&gt;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;code&gt;</span>and<span class="nt">&lt;/code&gt;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="nt">&lt;/names&gt;</span></div><div class='line' id='LC18'>&nbsp;&nbsp;...</div><div class='line' id='LC19'><span class="nt">&lt;/constellation&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1289371/be6d663022d3dba800bce32c13dd31e134c1792c/result.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1289371#file_result.xml" style="float:right;margin-right:10px;color:#666">result.xml</a>
            <a href="https://gist.github.com/1289371">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h4>Conclusion</h4>
<p>Now you have a Spring-based RESTful service created. As you can see there are not many code you must write. The most is configuration with annotation and some XML. With the @RequestMapping you can create powerful mappings from URL resources to controller methods. Another nice feature is to deliver the response as XML and JSON.</p>
<h4>Resources</h4>
<address>You will find all the code snippets at <a title="All the code snippets of this article" href="https://gist.github.com/1289371" target="_blank">https://gist.github.com/1289371</a></address>
]]></content:encoded>
			<wfw:commentRss>http://www.thorsten-kamann.de/2011/10/15/creating-restful-webservices-with-the-spring-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copy and paste code into Eclipse</title>
		<link>http://www.thorsten-kamann.de/2011/09/02/copy-and-paste-code-into-eclipse/</link>
		<comments>http://www.thorsten-kamann.de/2011/09/02/copy-and-paste-code-into-eclipse/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 09:20:01 +0000</pubDate>
		<dc:creator>thorque</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Eclipse]]></category>

		<guid isPermaLink="false">http://www.thorsten-kamann.de/?p=521</guid>
		<description><![CDATA[<a href="http://www.thorsten-kamann.de/2011/09/02/copy-and-paste-code-into-eclipse/" title="Copy and paste code into Eclipse"></a>There is a cool feature in Eclipse: You can paste code you copied from antoher website into a source folder of Eclipse and all needed resources will be created: Package structure Class/Interface/Enum Code will be pasted into the newly created &#8230;<p class="read-more"><a href="http://www.thorsten-kamann.de/2011/09/02/copy-and-paste-code-into-eclipse/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.thorsten-kamann.de/2011/09/02/copy-and-paste-code-into-eclipse/" title="Copy and paste code into Eclipse"></a><p>There is a cool feature in Eclipse: You can paste code you copied from antoher website into a source folder of Eclipse and all needed resources will be created:</p>
<ul>
<li>Package structure</li>
<li>Class/Interface/Enum</li>
<li>Code will be pasted into the newly created file</li>
</ul>
<div>For instance you&#8217;ll find this code anywhere in a tutorial:</div>
<pre lang="java">package a.b.c;</pre>
<pre lang="java">class A{
    public A(){}

    public void methodA(){
      System.out.println("This is a very useful class!");
    }
}</pre>
<div>Copy this code and paste it into an Eclipse source folder. After this a new package structure a/b/c and a new class A.java with the content exists.</div>
<div>

<a href='' title='Paste context menu in Eclipse'><img width="150" height="150" src="http://www.thorsten-kamann.de/wp-content/uploads/2011/09/eclipse_cp_1-150x150.png" class="attachment-thumbnail" alt="Paste context menu in Eclipse" title="Paste context menu in Eclipse" /></a>
<a href='' title='The copied source code'><img width="150" height="150" src="http://www.thorsten-kamann.de/wp-content/uploads/2011/09/eclipse_cp_2-150x150.png" class="attachment-thumbnail" alt="The copied source code" title="The copied source code" /></a>

</div>
<div>That&#8217;s all. A very useful feature&#8230;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thorsten-kamann.de/2011/09/02/copy-and-paste-code-into-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Agile Backoffice Series] Introduction</title>
		<link>http://www.thorsten-kamann.de/2011/06/23/agile-backoffice-series-introduction/</link>
		<comments>http://www.thorsten-kamann.de/2011/06/23/agile-backoffice-series-introduction/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 22:24:24 +0000</pubDate>
		<dc:creator>thorque</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Agile BackOffice]]></category>
		<category><![CDATA[Continuous Delivery]]></category>
		<category><![CDATA[Continuous Deployment]]></category>
		<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[Jenkins]]></category>
		<category><![CDATA[JIRA]]></category>

		<guid isPermaLink="false">http://www.thorsten-kamann.de/?p=509</guid>
		<description><![CDATA[<a href="http://www.thorsten-kamann.de/2011/06/23/agile-backoffice-series-introduction/" title="[Agile Backoffice Series] Introduction"></a>This article series describes the Agile Backoffice environment. STOP! Agile&#8230; what? Ok. Let us start with some details about the Agile Backoffice. The Agile Backoffice If you start working agile &#8211; mostly you use Scrum as first agile method &#8211; &#8230;<p class="read-more"><a href="http://www.thorsten-kamann.de/2011/06/23/agile-backoffice-series-introduction/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.thorsten-kamann.de/2011/06/23/agile-backoffice-series-introduction/" title="[Agile Backoffice Series] Introduction"></a><p>This article series describes the Agile Backoffice environment. STOP! Agile&#8230; what? Ok. Let us start with some details about the Agile Backoffice.</p>
<h3>The Agile Backoffice</h3>
<p>If you start working agile &#8211; mostly you use Scrum as first agile method &#8211; you and your team are very motivated to do this in a very agile way. The tasks are all pinned on a wall or a whiteboard, a continuous integration will be setup, the project know how will be shared in internal discussions. This works fine for a small team workingalways together in one location.</p>
<p>If you take a look around you see a lot of companies where this optimal conditions not exists. The teams are divided in different locations &#8211; different rooms, different floors, different locations. If your team splitted i such a way a whiteboard with pinned tasks are not a reasonable solution. Additionally to this you have a management interested in the progress of your work. For this requirements you need some tools supporting you. But you should use tools adaptable to your processes and not tools you must adaptable your processes to.</p>
<p>The central tool I choose for the Agile Backoffice is Atlassian Jira. It is the single proint of interest. In this tool you can see all requirements, the stories and the belonging tasks. Additionally you can get informations about changed (re)sources, running builds and &#8211; the most important &#8211; the status of a task, a story or requirement. You have a powerful reporting and a wide support for development tools: Eclipse, IntelliJ, Visual Studio.</p>
<p>Bit only with Jira you can&#8217;t get the result you wish. You need a continuous integration, a source control management, a repository for the deployable artifacts. Additionally you want to review and mesure the quality of your code. For this approaches there are tools but this tools should all be connected together, so there are based on the same data.</p>
<p>This is the goal of the Agile Backoffice and this series of articles. So stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thorsten-kamann.de/2011/06/23/agile-backoffice-series-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

