This guide walks you through the process of building an application that uses Spring for Android’s RestTemplate to consume XML from a RESTful web service.

What you’ll build

You’ll build a simple Android client that fetches some publicly visible Facebook data using their REST API.

What you’ll need

  • About 15 minutes

  • A favorite text editor or IDE

  • Android SDK

  • Maven 3.0 or later

  • An Android device or Emulator

How to complete this guide

Like most Spring 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:

When you’re finished, you can check your results against the code in gs-consuming-rest-xml-android/complete.

Set up the project

In this section you set up a basic build script and then create a simple application.

If you are new to Android projects, before you proceed, refer to Installing the Android Development Environment to help you configure your development environment.

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 Android Projects with Gradle or Building Android Projects with Maven.

Create the directory structure

In a project directory of your choosing, create the following subdirectory structure; for example, with the following command on Mac or Linux:

mkdir -p src/main/java/org/hello
└── src
    └── main
        └── java
            └── org
                └── hello


<project xmlns="" xmlns:xsi=""





Create an Android manifest

The Android Manifest contains all the information required to run an Android application, and it cannot build without one.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:label="@string/app_name" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


Add a text string. Text strings can be referenced from the application or from other resource files.


<?xml version="1.0" encoding="utf-8"?>
    <string name="app_name">Consuming REST XML</string>

Here you define the visual structure for the user interface of your application.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

Fetch a REST resource

Before you create a REST request, consider the data that you want your application to consume.

This project includes a simple, self-contained application for use with testing the REST request. When you query this service at the /hello-world endpoint, you receive an XML response. This response represents a greeting and resembles the following:

    <content>"Hello, World!"</content>

The id field is a unique identifier for the greeting, and content is the textual representation of the greeting.

Create a representation class

To model the greeting representation, you create a representation class:


package org.hello;

public class Greeting {

    private long id;
    private String content;

    public long getId() {
        return id;

    public void setId(long id) { = id;

    public String getContent() {
        return content;

    public void setContent(String content) {
        this.content = content;


Invoke a REST service with the RestTemplate

Spring provides a convenient template class called RestTemplate. RestTemplate makes interacting with most RESTful services a simple process. In the example below, you establish a few variables and then make a request of the simple REST service. You use the Simple XML library to marshal the XML response data into your representation classes.


package org.hello;

import org.springframework.http.converter.xml.SimpleXmlHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import android.os.Bundle;
import android.widget.TextView;

public class HelloActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {

    public void onStart() {
        final String url = "";

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

        Greeting greeting = restTemplate.getForObject(url, Greeting.class);

        TextView textView = (TextView) this.findViewById(;


Thus far, you’ve only used the HTTP verb GET to make calls, but you could just as easily have used POST, PUT, and so on.

Start the REST service

In order to consume a REST service, you must first have a REST service to consume. You can start the server, included in this guide, by running the following shell script from the service folder:


Build and run the client

With an attached device or emulator running, invoke the code and see the results of the REST request:

mvn clean package android:deploy android:run

The command builds the Android app and runs it in the emulator or attached device.


Congratulations! You have just developed a simple REST client using Spring.