https://github.com/openliberty/guide-rest-client-angularjs
A guide on how to consume a REST service with AngularJS: https://openliberty.io/guides/rest-client-angularjs.html
https://github.com/openliberty/guide-rest-client-angularjs
Last synced: 3 months ago
JSON representation
A guide on how to consume a REST service with AngularJS: https://openliberty.io/guides/rest-client-angularjs.html
- Host: GitHub
- URL: https://github.com/openliberty/guide-rest-client-angularjs
- Owner: OpenLiberty
- License: other
- Created: 2017-11-13T16:57:57.000Z (over 8 years ago)
- Default Branch: prod
- Last Pushed: 2026-02-01T11:33:17.000Z (4 months ago)
- Last Synced: 2026-02-27T04:27:07.182Z (4 months ago)
- Language: Java
- Homepage:
- Size: 323 KB
- Stars: 7
- Watchers: 5
- Forks: 12
- Open Issues: 4
-
Metadata Files:
- Readme: README.adoc
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
// Copyright (c) 2017, 2023 IBM Corporation and others.
// Licensed under Creative Commons Attribution-NoDerivatives
// 4.0 International (CC BY-ND 4.0)
// https://creativecommons.org/licenses/by-nd/4.0/
//
// Contributors:
// IBM Corporation
:projectid: rest-client-angularjs
:page-layout: guide-multipane
:page-duration: 20 minutes
:page-description: Explore how to access a simple RESTful web service and consume its resources with AngularJS in Open Liberty.
:page-releasedate: 2017-11-20
:page-related-guides: ['rest-intro', 'rest-client-java']
:page-permalink: /guides/{projectid}
:common-includes: https://raw.githubusercontent.com/OpenLiberty/guides-common/prod
:source-highlighter: prettify
:page-seo-title: Consuming a RESTful Java web service with AngularJS
:page-seo-description: A getting started tutorial with examples on how to access a RESTful Java micoservice and deserialize the returned JSON by using the high-level `$resource` service of AngularJS.
:guide-author: Open Liberty
= Consuming a RESTful web service with AngularJS
[.hidden]
NOTE: This repository contains the guide documentation source. To view the guide in published form, view it on the https://openliberty.io/guides/{projectid}.html[Open Liberty website].
Explore how to access a simple RESTful web service and consume its resources with AngularJS in Open Liberty.
// =================================================================================================
// Introduction
// =================================================================================================
== 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 [hotspot]`artists.json`.
artists.json
[source, json, linenums, role='code_column']
----
include::finish/src/resources/artists.json[]
----
// Static guide instruction
ifndef::cloud-hosted[]
You will implement an AngularJS client that consumes this JSON and displays its contents at the following URL: `\http://localhost:9080`.
endif::[]
// Cloud-hosted guide instruction
ifdef::cloud-hosted[]
You will implement an AngularJS client that consumes this JSON and displays its contents at a URL.
endif::[]
To learn more about REST services and how you can write them, see
https://openliberty.io/guides/rest-intro.html[Creating a RESTful web service^].
// =================================================================================================
// Getting Started
// =================================================================================================
[role='command']
include::{common-includes}/gitclone.adoc[]
=== 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.
// tag::runCommand[]
ifndef::cloud-hosted[]
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
```
endif::[]
ifdef::cloud-hosted[]
In this IBM cloud environment, you need to update the URL to access the ***artists.json*** in the ***consume-rest.js*** file. Run the following commands to go to the ***finish*** directory and update the ***consume-rest.js*** file:
```bash
cd finish
sed -i 's=http://localhost:9080/artists='"https://${USERNAME}-9080.$(echo $TOOL_DOMAIN | sed 's/\.labs\./.proxy./g')/artists"'=' src/main/webapp/js/consume-rest.js
```
To try out the application, run the following Maven goal to build the application and deploy it to Open Liberty:
```bash
mvn liberty:run
```
endif::[]
After you see the following message, your Liberty instance is ready:
[role="no_copy"]
----
The defaultServer server is ready to run a smarter planet.
----
// end::runCommand[]
ifndef::cloud-hosted[]
Navigate your browser to the application root http://localhost:9080[http://localhost:9080^] to see the following output:
endif::[]
ifdef::cloud-hosted[]
When the Liberty instance is running, select **Terminal** > **New Terminal** from the menu of the IDE to open another command-line session.
Open your browser and check out the application by going to the URL that the following command returns:
```bash
echo https://${USERNAME}-9080.$(echo $TOOL_DOMAIN | sed 's/\.labs\./.proxy./g')
```
See the following output:
endif::[]
[subs="quotes", role="no_copy"]
----
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:
----
[role='command']
include::{common-includes}/twyb-end.adoc[]
// =================================================================================================
// Starting the service
// =================================================================================================
== 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.
// cloud hosted instruction
ifdef::cloud-hosted[]
```bash
cd /home/project/guide-rest-client-angularjs/start
```
endif::[]
[role='command']
include::{common-includes}/devmode-lmp33-start.adoc[]
ifndef::cloud-hosted[]
After the Liberty instance is started, you can find your artist JSON at the following URL: http://localhost:9080/artists[http://localhost:9080/artists^].
endif::[]
// Cloud hosted guide instruction
ifdef::cloud-hosted[]
After the Liberty instance is started, run the following curl command to view your artist JSON.
```bash
curl -s http://localhost:9080/artists | jq
```
endif::[]
Any local changes to your JavaScript and HTML are picked up automatically, so you don't need to restart the Liberty instance.
// =================================================================================================
// Guide
// =================================================================================================
== 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.
[role="code_command hotspot", subs="quotes"]
----
#Create the `consume-rest` file.#
`src/main/webapp/js/consume-rest.js`
----
consume-rest.js
[source, javascript, linenums, role='code_column']
----
include::finish/src/main/webapp/js/consume-rest.js[]
----
ifdef::cloud-hosted[]
Run the following command to update the URL to access the ***artists.json*** in the ***consume-rest.js*** file:
```bash
sed -i 's=http://localhost:9080/artists='"https://${USERNAME}-9080.$(echo $TOOL_DOMAIN | sed 's/\.labs\./.proxy./g')/artists"'=' /home/project/guide-rest-client-angularjs/start/src/main/webapp/js/consume-rest.js
```
endif::[]
The application module is defined by [hotspot=consumeRestApp]`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 [hotspot=resource-module]`$resource` service.
The [hotspot=consumeRestApp]`ngResource` module is registered as it is appended after [hotspot=consumeRestApp]`consumeRestApp`. By registering another module, you are performing a dependency injection, exposing all functionalities of that module to your main application module.
Next, the [hotspot=Artists]`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 [hotspot=resource-module]`$resource` module that you imported earlier is the passed in function. Target the artist JSON URL in the [hotspot=resource-call]`$resource()` call.
The [hotspot=Controller]`controller` controls the flow of data in your application.Each controller is instantiated with its own isolated scope, accessible through the [hotspot=Scope]`$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.
[role="code_command hotspot file=0", subs="quotes"]
----
#Create the starting point of your application.#
`src/main/webapp/index.html`
----
index.html
[source, html, linenums, role='code_column tags=html hide_tags=copyright']
----
include::finish/src/main/webapp/index.html[]
----
consume-rest.js
[source, javascript, linenums, role='code_column']
----
include::finish/src/main/webapp/js/consume-rest.js[]
----
Before your application is bootstrapped, you must pull in two [hotspot=AngularJS file=0]`AngularJS` libraries and import [hotspot=consume-rest file=0]`consume-rest.js`.
The first import is the base AngularJS library, which defines the [hotspot=angular-script file=0]`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 [hotspot=angular-resource-script file=0]`angular-resource.js` script in your HTML. The application is bootstrapped because the [hotspot=consumeRestApp file=0]`consumeRestApp` application module is attached to the [hotspot=body file=0]`body` of the template.
Next, the [hotspot=ArtistCtrl file=0]`ArtistCtrl` controller is attached to the DOM to create a new child scope. The controller will make the [hotspot=Scope file=1]`artists` property of the [hotspot=Scope file=1]`$scope` object available to access at the point in the DOM where the controller is attached.
Once the controller is attached, the [hotspot=Artists file=0]`artists` property can be data-bounded to the template and accessed using the [hotspot=artist-info file=0]`{{ artists }}` expression. You can use the [hotspot=Artists file=0]`ng-repeat` directive to iterate over the contents of the [hotspot=Artists file=0]`artists` property.
ifndef::cloud-hosted[]
After everything is set up, point your browser to the application root http://localhost:9080[http://localhost:9080^] to see the following output:
endif::[]
ifdef::cloud-hosted[]
After everything is set up, open your browser and check out the application by going to the URL that the following command returns:
```bash
echo https://${USERNAME}-9080.$(echo $TOOL_DOMAIN | sed 's/\.labs\./.proxy./g')
```
See the following output:
endif::[]
[subs="quotes", role="no_copy"]
----
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.
ifndef::cloud-hosted[]
Whenever you change your AngularJS implementation, the application root at http://localhost:9080[http://localhost:9080^] will reflect the changes automatically. You can visit the root to manually check whether the artist JSON was consumed correctly.
endif::[]
ifdef::cloud-hosted[]
Whenever you change your AngularJS implementation, the application root at `http://accountname-9080.theiadocker-4.proxy.cognitiveclass.ai` will reflect the changes automatically. You can visit the root to manually check whether the artist JSON was consumed correctly.
endif::[]
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 https://docs.angularjs.org/guide/unit-testing[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.
include::{common-includes}/attribution.adoc[subs="attributes"]