GitHub has an RESTful JSON API that we can call from our apps. In this post I’ll show how I’m grabbing a GitHub repo’s watchers and pull them into my own app using Spring’s RestTemplate.
Meet the GitHub API
First, take a quick look at the GitHub Repo Watching API. It’s pretty simple, and it doesn’t require credentials to call. The URL for my Skybase project, for example, is https://api.github.com/repos/williewheeler/skybase/watchers.
Go ahead and click the watcher link above. You’ll see that the response is a JSON array of watcher objects.
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.
Create a User class for object/JSON mapping
We’re going to use Jackson to map the GitHub JSON to Java objects. We’ll need a User class for that. Here it is:
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; }
}
Nothing too amazing here. Note that I’m using @JsonProperty to clarify the mapping from the avatar_url and gravatar_id JSON fields to the avatarUrl and gravatarId Java properties, since Jackson doesn’t automatically handle underscores or anything like that.
The Maven dependency for the @JsonProperty annotation is
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.3</version>
</dependency>
Get the data using RestTemplate
Now that we have User, all we need to do is grab the data using RestTemplate. Easy:
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);
}
}
Recall that GitHub returns an array of watchers. That’s why we’re using User[].class in the call. RestTemplate substitutes the user and repo values into the GITHUB_WATCHER_URI string for us.
Spring configuration
All we need to do is put a RestTemplate instance on the app context:
<bean class="org.springframework.web.client.RestTemplate" />
Success!
After a little hacking at the UI:
[See my post Skybase/GitHub integration on the Skydingo blog for more context as to why we'd want to integrate our CMDB with GitHub.]


By Craig Walls January 14, 2012 - 4:26 pm
Good stuff, Willie! If you want to dig into using RestTemplate to talk with GitHub more, then I invite you to submit some pull requests to help flesh out the API binding for https://github.com/SpringSource/spring-social-github.
By Willie Wheeler January 14, 2012 - 4:31 pm
Hey, that’s awesome. Wasn’t aware that you were doing GitHub integration, but will definitely take a look. Thanks Craig.
By Calling the GitHub API using Spring’s RestTemplate | Web Services | Syngu January 16, 2012 - 11:48 pm
[...] Shows how to use Spring's RestTemplate to make web service calls against the GitHub API v3. Eventually Spring Social GitHub will support this, but for now, RestTemplate is about as easy as it gets. Web Services Read the original post on DZone… [...]