Consuming a RESTful web service with AngularJS

duration 20 minutes


Explore how to access a simple RESTful web service and consume its resources with AngularJS in Open Liberty.

What you’ll learn

You will learn how to access a REST service and deserialize the returned JSON that contains a list of artists and their albums by using the high-level $resource service of AngularJS.

The REST service that provides the artists and albums resource was written for you in advance and responds with the artists.json.


 2    {
 3        "name": "foo",
 4        "albums": [
 5            {
 6                "title": "album_one",
 7                "artist": "foo",
 8                "ntracks": 12
 9            },
10            {
11                "title": "album_two",
12                "artist": "foo",
13                "ntracks": 15
14            }
15        ]
16    },
17    {
18        "name": "bar",
19        "albums": [
20            {
21                "title": "foo walks into a bar",
22                "artist": "bar",
23                "ntracks": 12
24            }
25        ]
26    },
27    {
28        "name": "dj",
29        "albums": [
30        ]
31    }

You will implement an AngularJS client that consumes this JSON and displays its contents at the following URL: http://localhost:9080.

To learn more about REST services and how you can write them, see Creating a RESTful web service.

Getting started

The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:

git clone
cd guide-rest-client-angularjs

The start directory contains the starting project that you will build upon.

The finish directory contains the finished project that you will build.

Starting the service

Before you begin the implementation, start the provided REST service so that the artist JSON is available to you.

To start the REST service, run the Maven install and liberty:start-server goals from the start directory:

mvn install liberty:start-server

After you start the server, you can find your artist JSON at the following URL: http://localhost:9080/artists.

Any local changes to your JavaScript and HTML are picked up automatically. After you start the Open Liberty server, you do not need to restart it.

Creating the AngularJS controller

Navigate to the start directory to begin.

Begin by registering your application module. Every application must contain at least one module, the application module, which will be bootstrapped to launch the application.

Create the consume-rest file.


 1var app = angular.module('consumeRestApp', ['ngResource']);
 3app.factory("artists", function($resource) {
 4    return $resource("http://localhost:9080/artists");
 7app.controller("ArtistsCtrl", function($scope, artists) {
 8    artists.query(function(data) {
 9        $scope.artists = data;
10    }, function(err) {
11        console.error("Error occured: ", err);
12    });

The application module is defined by consumeRestApp.

Your application will need some way of communicating with RESTful web services in order to retrieve their resources. In the case of this guide, your application will need to communicate with the artists service to retrieve the artists JSON. While there exists a variety of ways of doing this, you can use the fairly straightforward AngularJS $resource service.

The ngResource module is registered as it is appended after consumeRestApp. By registering another module, you are performing a dependency injection, exposing all functionalities of that module to your main application module.

Next, the Artists AngularJS service is defined by using the Factory recipe. The Factory recipe constructs a new service instance with the return value of a passed in function. In this case, the $resource module that you imported earlier is the passed in function. Target the artist JSON URL in the $resource() call.

The controller controls the flow of data in your application.Each controller is instantiated with its own isolated scope, accessible through the $scope parameter. All data that is bound to this parameter is available in the view to which the controller is attached.

You can now access the artists property from the template at the point in the Document Object Model (DOM) where the controller is registered.

Creating the AngularJS template

You will create the starting point of your application. This file will contain all elements and attributes specific to AngularJS.

Create the starting point of your application.


 1<!DOCTYPE html>
 3    <head>
 4        <script
 5            src=''/>
 6        </script>
 7        <script
 8           src=''>
 9        </script>
10        <script src='./js/consume-rest.js'></script>
11    </head>
12    <body ng-app='consumeRestApp'>
13        <div ng-controller='ArtistsCtrl'>
14            <div ng-repeat='artist in artists'>
15                <p>{{ }} wrote {{ artist.albums.length }} albums:</p>
16                <div ng-repeat='album in artist.albums'>
17                    <p style='text-indent: 20px'>
18                        Album titled <b>{{ album.title }}</b> by
19                                     <b>{{ album.artist }}</b> contains
20                                     <b>{{ album.ntracks }}</b> tracks
21                    </p>
22                </div>
23            </div>
24        </div>
25    </body>


 1var app = angular.module('consumeRestApp', ['ngResource']);
 3app.factory("artists", function($resource) {
 4    return $resource("http://localhost:9080/artists");
 7app.controller("ArtistsCtrl", function($scope, artists) {
 8    artists.query(function(data) {
 9        $scope.artists = data;
10    }, function(err) {
11        console.error("Error occured: ", err);
12    });

Before your application is bootstrapped, you must pull in two AngularJS libraries and import consume-rest.js.

The first import is the base AngularJS library. The second library is responsible for providing the APIs for the $resource service. The application is bootstrapped because the consumeRestApp application module is attached to the body of the template.

Next, the ArtistCtrl controller is attached to the DOM to create a new child scope. The controller will make the artists property of the $scope object available to access at the point in the DOM where the controller is attached.

Once the controller is attached, the artists property can be data-bounded to the template and accessed using the {{ artists }} expression. You can use the ng-repeat directive to iterate over the contents of the artists property.

After everything is set up, point your browser to the application root http://localhost:9080 to see the following output:

foo wrote 2 albums:
    Album titled album_one by foo contains 12 tracks
    Album tilted album_two by foo contains 15 tracks
bar wrote 1 albums:
    Album titled foo walks into a bar by bar contains 12 tracks
dj wrote 0 albums:

Testing the AngularJS client

No explicit code directly uses the consumed artist JSON, so you do not need to write any test cases for this guide.

Whenever you change your AngularJS implementation, the application root at http://localhost:9080 will reflect the changes automatically. You can visit the root to manually check whether the artist JSON was consumed correctly.

When you are done checking the application root, stop the Open Liberty server by running the following command:

mvn liberty:stop-server

Although this guide did not teach you how to build logic, you will likely build logic when you develop your own applications, and testing will become a crucial part of your development lifecycle. If you need to write test cases, follow the official unit testing and end-to-end testing documentation on the official AngularJS page.

Great work! You’re done!

You have just accessed a simple RESTful web service and consumed its resources by using AngularJS in Open Liberty.

Guide Attribution

Consuming a RESTful web service with AngularJS by Open Liberty is licensed under CC BY-ND 4.0

Copied to clipboard
Copy code block
Copy file contents


Nice work! Where to next?

What did you think of this guide?

Extreme Dislike Dislike Like Extreme Like

What could make this guide better?

Raise an issue to share feedback

Create a pull request to contribute to this guide

Need help?

Ask a question on Stack Overflow

Like Open Liberty? Star our repo on GitHub.