<?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>Spring in Practice &#187; REST</title>
	<atom:link href="http://springinpractice.com/tag/rest/feed/" rel="self" type="application/rss+xml" />
	<link>http://springinpractice.com</link>
	<description>Willie Wheeler&#039;s Spring blog</description>
	<lastBuildDate>Sun, 22 Jan 2012 07:11:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='springinpractice.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Spring in Practice &#187; REST</title>
		<link>http://springinpractice.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://springinpractice.com/osd.xml" title="Spring in Practice" />
	<atom:link rel='hub' href='http://springinpractice.com/?pushpress=hub'/>
		<item>
		<title>Calling the GitHub API using Spring&#8217;s RestTemplate</title>
		<link>http://springinpractice.com/2012/01/14/calling-the-github-api-using-springs-resttemplate/</link>
		<comments>http://springinpractice.com/2012/01/14/calling-the-github-api-using-springs-resttemplate/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 22:15:59 +0000</pubDate>
		<dc:creator>Willie Wheeler</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[github api]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[resttemplate]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://springinpractice.com/?p=552</guid>
		<description><![CDATA[GitHub has an RESTful JSON API that we can call from our apps. In this post I&#8217;ll show how I&#8217;m &#8230;<p><a href="http://springinpractice.com/2012/01/14/calling-the-github-api-using-springs-resttemplate/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=springinpractice.com&amp;blog=4620434&amp;post=552&amp;subd=springinpractice&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>GitHub has an <a title="GitHub API" href="http://developer.github.com/">RESTful JSON API</a> that we can call from our apps. In this post I&#8217;ll show how I&#8217;m grabbing a GitHub repo&#8217;s watchers and pull them into my own app using Spring&#8217;s RestTemplate.</p>
<h3>Meet the GitHub API</h3>
<p>First, take a quick look at the <a title="GitHub Repo Watching API" href="http://developer.github.com/v3/repos/watching/">GitHub Repo Watching API</a>. It&#8217;s pretty simple, and it doesn&#8217;t require credentials to call. The URL for my <a title="Skybase GitHub site" href="https://github.com/williewheeler/skybase">Skybase</a> project, for example, is <a title="Skybase watchers" href="https://api.github.com/repos/williewheeler/skybase/watchers">https://api.github.com/repos/williewheeler/skybase/watchers</a>.</p>
<p>Go ahead and click the watcher link above. You&#8217;ll see that the response is a JSON array of watcher objects.</p>
<p>Note that since the GitHub API is HTTPS-based, you will need to download the GitHub API certificate (using, e.g., openssl) and then import it into your Java keystore (using keytool) so your Java programs can call the API without running into certificate errors.</p>
<h3>Create a User class for object/JSON mapping</h3>
<p>We&#8217;re going to use <a title="Jackson JSON processor" href="http://jackson.codehaus.org/">Jackson</a> to map the GitHub JSON to Java objects. We&#8217;ll need a <code>User</code> class for that. Here it is:</p>
<pre>package org.skydingo.skybase.integrations.github.model;

import org.codehaus.jackson.annotate.JsonProperty;

public class User {
    private Long id;
    private String url, login, avatarUrl, gravatarId;

    public Long getId() { return id; }

    public void setId(Long id) { this.id = id; }

    public String getUrl() { return url; }

    public void setUrl(String url) { this.url = url; }

    public String getLogin() { return login; }

    public void setLogin(String login) { this.login = login; }

    @JsonProperty("avatar_url")
    public String getAvatarUrl() { return avatarUrl; }

    public void setAvatarUrl(String avatarUrl) { this.avatarUrl = avatarUrl; }

    @JsonProperty("gravatar_id")
    public String getGravatarId() { return gravatarId; }

    public void setGravatarId(String gravatarId) { this.gravatarId = gravatarId; }
}</pre>
<p>Nothing too amazing here. Note that I&#8217;m using <code>@JsonProperty</code> to clarify the mapping from the <code>avatar_url</code> and <code>gravatar_id</code> JSON fields to the <code>avatarUrl</code> and <code>gravatarId</code> Java properties, since Jackson doesn&#8217;t automatically handle underscores or anything like that.</p>
<p>The Maven dependency for the <code>@JsonProperty</code> annotation is</p>
<pre>&lt;dependency&gt;
    &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
    &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
    &lt;version&gt;1.9.3&lt;/version&gt;
&lt;/dependency&gt;</pre>
<h3>Get the data using RestTemplate</h3>
<p>Now that we have <code>User</code>, all we need to do is grab the data using <code>RestTemplate</code>. Easy:</p>
<pre>package org.skydingo.skybase.service;

import javax.inject.Inject;
import org.skydingo.skybase.integrations.github.model.User;
import org.skydingo.skybase.model.Application;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApplicationService {
    private static final String GITHUB_WATCHER_URI =
        "https://api.github.com/repos/{user}/{repo}/watchers";

    @Inject private RestTemplate restTemplate;

    public User[] getGithubRepoWatchers(String user, String repo) {
        return restTemplate.getForObject(GITHUB_WATCHER_URI, User[].class, user, repo);
    }
}</pre>
<p>Recall that GitHub returns an array of watchers. That&#8217;s why we&#8217;re using <code>User[].class</code> in the call. <code>RestTemplate</code> substitutes the <code>user</code> and <code>repo</code> values into the <code>GITHUB_WATCHER_URI</code> string for us.</p>
<h3>Spring configuration</h3>
<p>All we need to do is put a <code>RestTemplate</code> instance on the app context:</p>
<p><code>&lt;bean class="org.springframework.web.client.RestTemplate" /&gt;</code></p>
<h3>Success!</h3>
<p>After a little hacking at the UI:</p>
<div id="attachment_570" class="wp-caption alignnone" style="width: 539px"><a href="http://springinpractice.files.wordpress.com/2012/01/skybase_scm.png"><img class="size-full wp-image-570" title="skybase_scm" src="http://springinpractice.files.wordpress.com/2012/01/skybase_scm.png?w=529&#038;h=296" alt="" width="529" height="296" /></a><p class="wp-caption-text">Skybase SCM page</p></div>
<p>[See my post <a href="http://skydingo.com/blog/?p=445">Skybase/GitHub integration</a> on the <a href="http://skydingo.com/blog/">Skydingo blog</a> for more context as to why we'd want to integrate our CMDB with GitHub.]</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/springinpractice.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/springinpractice.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/springinpractice.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/springinpractice.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/springinpractice.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/springinpractice.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/springinpractice.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/springinpractice.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/springinpractice.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/springinpractice.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/springinpractice.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/springinpractice.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/springinpractice.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/springinpractice.wordpress.com/552/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=springinpractice.com&amp;blog=4620434&amp;post=552&amp;subd=springinpractice&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://springinpractice.com/2012/01/14/calling-the-github-api-using-springs-resttemplate/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b0a72ae61c5c74a51fe46cf66599b6c5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">williewheeler</media:title>
		</media:content>

		<media:content url="http://springinpractice.files.wordpress.com/2012/01/skybase_scm.png" medium="image">
			<media:title type="html">skybase_scm</media:title>
		</media:content>
	</item>
		<item>
		<title>New stuff in Spring 3.0</title>
		<link>http://springinpractice.com/2008/12/02/new-stuff-in-spring-30/</link>
		<comments>http://springinpractice.com/2008/12/02/new-stuff-in-spring-30/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 02:59:26 +0000</pubDate>
		<dc:creator>Willie Wheeler</dc:creator>
				<category><![CDATA[SpringOne]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JEE]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Spring 3.0]]></category>
		<category><![CDATA[Spring EL]]></category>
		<category><![CDATA[Spring Web MVC]]></category>

		<guid isPermaLink="false">http://springinpractice.wordpress.com/?p=159</guid>
		<description><![CDATA[This morning I attended a talk by Juergen Hoeller on what&#8217;s coming in Spring 3.0. First, he said that we &#8230;<p><a href="http://springinpractice.com/2008/12/02/new-stuff-in-spring-30/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=springinpractice.com&amp;blog=4620434&amp;post=159&amp;subd=springinpractice&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This morning I attended a talk by Juergen Hoeller on what&#8217;s coming in Spring 3.0. First, he said that we can expect the first milestone release sometime this week, and said that he expected there to be about three milestone releases I think. RC1 is supposed to be out around March 2009 and final around April 2009.</p>
<p>Echoing something I&#8217;ve heard Keith Donald say, Juergen said that Spring 3.0 is intended to complete the work that was started in Spring 2.5. Keith described the release as being &#8220;evolutionary, not revolutionary.&#8221; That may be, but there&#8217;s still quite a bit to be excited about.  Here&#8217;s what I got from the talk today. (Hold onto your hats because I took good notes.)</p>
<h3>Java and JEE compatibility</h3>
<p>Spring 3.0 will be the first release that requires Java 5+. From SpringSource&#8217;s perspective this is nice because they can avail themselves to generics, varargs, etc. But from the app developer&#8217;s perspective, the idea is that the platform is really pushing use of annotations as the preferred way of doing configuration. So that means Java 5+.</p>
<p>Spring 3.0 will continue to be compatible with J2EE 1.4 and JEE 5. Also, as JEE 6 matures, Spring 3.0 will try to provide early support, but progress on JEE 6 has been somewhat slow and Spring 3.0 should be out before JEE 6. So Spring 3.1 and 3.2 will probably continue work in this direction. Anyway, SpringSource plans to support JSF 2.0, JPA 2.0, Servlet 3.0 when it&#8217;s ready, etc.)</p>
<h3>Spring EL (Unified EL++)</h3>
<p>Juergen described support for expression languages as probably the most important and powerful feature in Spring 3.0. It will be a proprietary EL parser implementation&#8211;one of the few new modules that Spring 3.0 will introduce&#8211;and it sounded like it would like Unified EL but on steroids. They&#8217;re trying to keep the EL pretty similar to how it works in JSF, and Spring&#8217;s EL is designed to be stylistically and syntactically compatible with Unified EL. Also, even though right now Spring Web Flow 2 uses external EL implementations (JBoss EL implementation of Unified EL, I think, or else OGNL), SWF 3 will use Spring EL instead of an external implementation.</p>
<p>Here&#8217;s a code example he gave. This one is for EL in bean definitions:</p>
<pre>&lt;bean class="mycompany.RewardsTestDatabase"&gt;
    &lt;property name="databaseName" value="#{systemProperties.databaseName}"/&gt;
    &lt;property name="keyGenerator" value="#{strategyBean.databaseKeyGenerator}"/&gt;
&lt;/bean&gt;</pre>
<p>The idea is this. Just like in other ELs, you will have some implicit objects&#8211;objects that you can reference in your EL either because the objects are global in scope or else because the object lives in your current scope (e.g., request scope, session scope, etc.). So in the example above, <code>systemProperties</code> is a global implicit object, and <code>strategyBean</code> is a bean name. Bean names will be available as EL objects then.</p>
<p>I&#8217;m assuming that the <code>databaseName</code> and <code>keyGenerator</code> properties will be evaluated each time they are accessed. Otherwise it wouldn&#8217;t be much of an expression language. I&#8217;m kind of curious to see how they do this. My guess is that the approach will be to proxy beans with EL-based properties, with the proxies evaluating the reference upon demand.</p>
<p>Juergen gave another example, this time of EL in component annotations. Here it is:</p>
<pre>
@Repository
public class RewardsTestDatabase {

    @Value("#{systemProperties.databaseName}")
    public void setDatabaseName(String dbName) { ... }

    @Value("#{strategyBean.databaseKeyGenerator}")
    public void setKeyGenerator(KeyGenerator kg) { ... }
}
</pre>
<p>I <i>believe</i> these two configuration examples are different in a subtle way. The value of the <code>@Value</code> attribute is a default value for the property if a value isn&#8217;t explicitly defined in the app context. One practical benefit we get has to do with how component scanning currently works. When we component scan, we depend on autowiring to establish injected references. The problem is that if our dependencies are primitive types, autowiring is no longer an option and so we have to go explicit with our bean definitions. With EL-based defaults, we can keep the component scanning, and just set the primitive types using a bean, or maybe using system properties, etc.</p>
<h3>REST support</h3>
<p>Another big topic in Spring 3.0 is REST support. Spring Web MVC will provide first-class support for RESTful web services, and not necessarily just XML-based. Keith was saying that there will be a JSON view as well. So the web services would be useful not only in integration efforts but also for example when you have JavaScript widgets that want to make AJAX calls back to the server and get JSON. Anyway, Spring Web MVC&#8217;s current design is such that it&#8217;s really easy to tweak it a bit to support REST, and that&#8217;s what they&#8217;re doing. So one aspect of this would be to continue to use <code>@RequestMapping</code> to specify an HTTP method, such as <code>RequestMethod.PUT</code>.  Another piece would be to introduce a new <code>@PathVariable</code> annotation that works pretty much like <code>@RequestParam</code> currently works, but handles URL path segments instead of HTTP parameters. Here&#8217;s a code example that shows how it will work:</p>
<pre>
@RequestMapping(value = "/show/{id}", method = GET)
public Reward show(@PathVariable("id") long id) {
    return this.rewardsAdminService.findReward(id);
}
</pre>
<p>I like it!</p>
<p>Another feature of the REST support is support for different representations, i.e. content negotiation. They want to use the HTTP <code>Accept</code> header as a new way to do view resolution. So presumably there will be a new <code>ViewResolver</code> implementation that looks at the <code>Accept</code> header and returns, say, an XML view (accepts <code>application/xml</code>), a JSON view (accepts <code>application/json</code>) or an ATOM view (accepts <code>application/atom+xml</code>) according to that header. This would be the preferred way of doing view resolution for the hardcore REST purists. But Spring Web MVC would also support extension-based resolution; e.g. <code>.json</code> maps to a <code>JsonView</code>, etc.</p>
<h3>Some @MVC refinements</h3>
<p>They&#8217;re planning to add a couple of new @MVC annotations besides <code>@RequestParam</code> (which already exists) and the new <code>@PathVariable</code> annotation. These are:</p>
<ul>
<li><code>@RequestHeader</code>: Grab HTTP request header data.</li>
<li><code>@CookieValue</code>: Grab cookie value. (Surprise!)</li>
</ul>
<p>These will allow us to get at the data without having to declare the raw <code>HttpServletRequest</code> as we currently have to do.  Here&#8217;s a code sample:</p>
<pre>
@RequestMapping("/show")
public Reward show(@RequestHeader("region") long regionId,
    @CookieValue("language") String langId) {

    ...
}
</pre>
<p>There&#8217;s quite a bit more, but this post is pretty long as it is and I need to hit the sack. I&#8217;ll try to post a little more tomorrow.</p>
<p>[Update: Here's <a href="http://springinpractice.wordpress.com/2008/12/03/new-stuff-in-spring-30-part-2/">part 2</a>.]</p>
<p>[Update: Here's <a href="http://www.infoq.com/news/2008/12/springone-2008">a related InfoQ article</a>.]</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/springinpractice.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/springinpractice.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/springinpractice.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/springinpractice.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/springinpractice.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/springinpractice.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/springinpractice.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/springinpractice.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/springinpractice.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/springinpractice.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/springinpractice.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/springinpractice.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/springinpractice.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/springinpractice.wordpress.com/159/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=springinpractice.com&amp;blog=4620434&amp;post=159&amp;subd=springinpractice&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://springinpractice.com/2008/12/02/new-stuff-in-spring-30/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b0a72ae61c5c74a51fe46cf66599b6c5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">williewheeler</media:title>
		</media:content>
	</item>
	</channel>
</rss>
