This guide walks you through the process of creating an application that consumes a RESTful web service.
You'll build an application that uses Spring's RestTemplate
to retrieve a company's page data from Facebook's Graph API at:
http://graph.facebook.com/gopivotal
- About 15 minutes
- A favorite text editor or IDE
- JDK 6 or later
- Gradle 1.8+ or Maven 3.0+
- You can also import the code from this guide as well as view the web page directly into Spring Tool Suite (STS) and work your way through it from there.
Like all Spring's Getting Started guides, you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.
To start from scratch, move on to Set up the project.
To skip the basics, do the following:
- [Download][zip] and unzip the source repository for this guide, or clone it using [Git][u-git]:
git clone https://github.com/spring-guides/gs-consuming-rest.git
- cd into
gs-consuming-rest/initial
. - Jump ahead to Fetch a REST resource.
When you're finished, you can check your results against the code in gs-consuming-rest/complete
.
[zip]: https://github.com/spring-guides/gs-consuming-rest/archive/master.zip
[u-git]: /understanding/Git
First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Gradle and Maven is included here. If you're not familiar with either, refer to Building Java Projects with Gradle or Building Java Projects with Maven.
In a project directory of your choosing, create the following subdirectory structure; for example, with mkdir -p src/main/java/hello
on *nix systems:
└── src
└── main
└── java
└── hello
Below is the initial Gradle build file. But you can also use Maven. The pom.xml file is included right here. If you are using Spring Tool Suite (STS), you can import the guide directly.
build.gradle
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
jar {
baseName = 'gs-consuming-rest'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter:0.5.0.M5")
compile("org.springframework:spring-web:4.0.0.M3")
compile("org.codehaus.jackson:jackson-mapper-asl:1.9.13")
testCompile("junit:junit:4.11")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.8'
}
Note: This guide is using Spring Boot.
With project setup complete, you can create a simple application that consumes a RESTful service.
Suppose that you want to find out what Facebook knows about Pivotal. Knowing that Pivotal has a page on Facebook and that the ID is "gopivotal", you should be able to query Facebook's Graph API via this URL:
http://graph.facebook.com/gopivotal
If you request that URL through your web browser or curl, you'll receive a JSON document that looks something like this:
{
"id": "161112704050757",
"about": "At Pivotal, our mission is to enable customers to build a new class of applications, leveraging big and fast data, and do all of this with the power of cloud independence. ",
"app_id": "0",
"can_post": false,
"category": "Internet/software",
"checkins": 0,
"cover": {
"cover_id": 163344023827625,
"source": "http://sphotos-d.ak.fbcdn.net/hphotos-ak-frc1/s720x720/554668_163344023827625_839302172_n.png",
"offset_y": 0,
"offset_x": 0
},
"founded": "2013",
"has_added_app": false,
"is_community_page": false,
"is_published": true,
"likes": 126,
"link": "https://www.facebook.com/gopivotal",
"location": {
"street": "1900 South Norfolk St.",
"city": "San Mateo",
"state": "CA",
"country": "United States",
"zip": "94403",
"latitude": 37.552261,
"longitude": -122.292152
},
"name": "Pivotal",
"phone": "650-286-8012",
"talking_about_count": 15,
"username": "gopivotal",
"website": "http://www.gopivotal.com",
"were_here_count": 0
}
Easy enough, but not terribly useful when fetched through a browser or through curl.
A more useful way to consume a REST web service is programmatically. To help you with that task, Spring provides a convenient template class called RestTemplate
. RestTemplate
makes interacting with most RESTful services a one-line incantation. And it can even bind that data to custom domain types.
First, create a domain class to contain the data that you need. If all you need to know are Pivotal's name, phone number, website URL, and what the gopivotal page is about, then the following domain class should do fine:
src/main/java/hello/Page.java
package hello;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class Page {
private String name;
private String about;
private String phone;
private String website;
public String getName() {
return name;
}
public String getAbout() {
return about;
}
public String getPhone() {
return phone;
}
public String getWebsite() {
return website;
}
}
As you can see, this is a simple Java class with a handful of properties and matching getter methods. It's annotated with @JsonIgnoreProperties
from the Jackson JSON processing library to indicate that any properties not bound in this type should be ignored.
Although it is possible to package this service as a traditional [WAR][u-war] file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java main()
method. Along the way, you use Spring's support for embedding the [Tomcat][u-tomcat] servlet container as the HTTP runtime, instead of deploying to an external instance.
Now you can write the Application
class that uses RestTemplate
to fetch the data from Pivotal's page at Facebook into a Page
object.
src/main/java/hello/Application.java
package hello;
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
Page page = restTemplate.getForObject("http://graph.facebook.com/gopivotal", Page.class);
System.out.println("Name: " + page.getName());
System.out.println("About: " + page.getAbout());
System.out.println("Phone: " + page.getPhone());
System.out.println("Website: " + page.getWebsite());
}
}
Because the Jackson JSON processing library is in the classpath, RestTemplate
will use it (via a message converter) to convert the incoming JSON data into a Page
object. From there, the contents of the Page
object will be printed to the console.
Here you've only used RestTemplate
to make an HTTP GET
request. But RestTemplate
also supports other HTTP verbs such as POST
, PUT
, and DELETE
.
Now that your Application
class is ready, you simply instruct the build system to create a single, executable jar containing everything. This makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.
Below are the Gradle steps, but if you are using Maven, you can find the updated pom.xml right here and build it by typing mvn clean package
.
Update your Gradle build.gradle
file's buildscript
section, so that it looks like this:
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M4")
}
}
Further down inside build.gradle
, add the following to the list of applied plugins:
apply plugin: 'spring-boot'
You can see the final version of build.gradle
[right here]((https://github.com/spring-guides/gs-consuming-rest/blob/master/complete/build.gradle).
The Spring Boot gradle plugin collects all the jars on the classpath and builds a single "über-jar", which makes it more convenient to execute and transport your service.
It also searches for the public static void main()
method to flag as a runnable class.
Now run the following command to produce a single executable JAR file containing all necessary dependency classes and resources:
$ ./gradlew build
If you are using Gradle, you can run the JAR by typing:
$ java -jar build/libs/gs-consuming-rest-0.1.0.jar
If you are using Maven, you can run the JAR by typing:
$ java -jar target/gs-consuming-rest-0.1.0.jar
Note: The procedure above will create a runnable JAR. You can also opt to build a classic WAR file instead.
If you are using Gradle, you can run your service at the command line this way:
$ ./gradlew clean build && java -jar build/libs/gs-consuming-rest-0.1.0.jar
Note: If you are using Maven, you can run your service by typing
mvn clean package && java -jar target/gs-consuming-rest-0.1.0.jar
.
You should see the following output:
Name: Pivotal
About: At Pivotal, our mission is to enable customers to build a new class of applications, leveraging big and fast data, and do all of this with the power of cloud independence.
Phone: 650-286-8012
Website: http://www.gopivotal.com
Congratulations! You have just developed a simple REST client using Spring.