Skip to content

OpenLiberty/guide-rest-client-angularjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Consuming a RESTful web service with AngularJS

Note
This repository contains the guide documentation source. To view the guide in published form, view it on the Open Liberty website.

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.

artists.json

link:finish/src/resources/artists.json[role=include]

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.

Try what you’ll build

The finish directory in the root of this guide contains the finished application. Give it a try before you proceed.

To try out the application, first go to the finish directory and run the following Maven goal to build the application and deploy it to Open Liberty:

cd finish
mvn liberty:run

After you see the following message, your Liberty instance is ready:

The defaultServer server is ready to run a smarter planet.

Navigate 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:

Starting the service

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

Navigate to the start directory to begin.

After the Liberty instance is started, 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, so you don’t need to restart the Liberty instance.

Creating the AngularJS controller

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.
src/main/webapp/js/consume-rest.js

consume-rest.js

link:finish/src/main/webapp/js/consume-rest.js[role=include]

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.
src/main/webapp/index.html

index.html

link:finish/src/main/webapp/index.html[role=include]

consume-rest.js

link:finish/src/main/webapp/js/consume-rest.js[role=include]

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, which defines the angular.js script in your HTML. The second import is the library responsible for providing the APIs for the $resource service, which also defines the angular-resource.js script in your HTML. 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, exit dev mode by pressing CTRL+C in the command-line session where you ran the Liberty.

When you develop your own applications, testing becomes 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 website.

Great work! You’re done!

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