/guide-rest-client-reactjs

A guide on how to access a simple RESTful web service and consume its resources with ReactJS in Open Liberty.

Primary LanguageHTMLOtherNOASSERTION

Consuming a RESTful web service with ReactJS

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 ReactJS 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 an HTTP client with the ReactJS library. You will then present this data by using a ReactJS paginated table component.

ReactJS is a JavaScript library that is used to build user interfaces. Its main purpose is to incorporate a component-based approach to create reusable UI elements. With ReactJS, you can also interface with other libraries and frameworks. Note that the names ReactJS and React are used interchangeably.

The React application in this guide is provided and configured for you in the src/main/frontend directory. The application uses the Create React App prebuilt configuration to set up the modern single-page React application. The create-react-app integrated toolchain is a comfortable environment for learning React and is the best way to start building a new single-page application with React.

artists.json

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

The REST service that provides the resources was written for you in advance in the back end of the application, and it responds with the artists.json in the src/resources directory. You will implement a ReactJS client as the front end of your application, which consumes this JSON file and displays its contents on a single-page webpage.

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

Try what you’ll build

The finish directory in the root of this guide contains the finished application. The React front end is already pre-built for you and the static files from the production build can be found in the src/main/webapp/static directory.

To try out the application, navigate 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 application Liberty instance is ready:

The defaultServer server is ready to run a smarter planet.

Next, point your browser to the http://localhost:9080 web application root to see the following output:

React Paginated Table

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 you start the service, you can find your artist JSON at the http://localhost:9080/artists URL.

All the dependencies for the React front end can be found in src/main/frontend/src/package.json, and they are installed before the front end is built by the frontend-maven-plugin. Additionally, some provided CSS stylesheets files are provided and can be found in the src/main/frontend/src/Styles directory.

Project configuration

The front end of your application uses Node.js to build your React code. The Maven project is configured for you to install Node.js and produce the production files, which are copied to the web content of your application.

Node.js is a server-side JavaScript runtime that is used for developing networking applications. Its convenient package manager, npm, is used to run the React build scripts that are found in the package.json file. To learn more about Node.js, see the official Node.js documentation.

The frontend-maven-plugin is used to install the dependencies that are listed in your package.json file from the npm registry into a folder called node_modules. The node_modules folder can be found in your working directory. Then, the configuration produces the production files to the src/main/frontend/build directory.

The maven-resources-plugin copies the static content from the build directory to the web content of the application.

pom.xml

link:finish/pom.xml[role=include]

Creating the default page

You need to create the entry point of your React application. create-react-app uses the index.js file as the main entry point of the application. This JavaScript file corresponds with the index.html file, which is the entry point where your code runs in the browser.

Create the index.js file.
src/main/frontend/src/index.js

index.js

link:finish/src/main/frontend/src/index.js[role=include]

index.html

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

The React library imports the react package. A DOM, or Document Object Model, is a programming interface for HTML and XML documents. React offers a virtual DOM, which is essentially a copy of the browser DOM that resides in memory. The React virtual DOM improves the performance of your web application and plays a crucial role in the rendering process. The react-dom package provides DOM-specific methods that can be used in your application to get outside of the React model, if necessary.

The render method takes an HTML DOM element and tells the ReactDOM to render your React application inside of this DOM element. To learn more about the React virtual DOM, see the ReactDOM documentation.

Creating the React components

A React web application is a collection of components, and each component has a specific function. You will create the components that are used in the application to acquire and display data from the REST API.

The main component in your React application is the App component. You need to create the App.js file to act as a container for all other components.

Create the App.js file.
src/main/frontend/src/Components/App.js

App.js

link:finish/src/main/frontend/src/Components/App.js[role=include]

The App.js file returns the ArtistTable function to create a reusable element that encompasses your web application.

Next, create the ArtistTable function that fetches data from your back end and renders it in a table.

Create the ArtistTable.js file.
src/main/frontend/src/Components/ArtistTable.js

ArtistTable.js

link:finish/src/main/frontend/src/Components/ArtistTable.js[role=include]

The React library imports the react package for you to create the ArtistTable function. This function must have the export declaration because it is being exported to the App.js module. The posts object is initialized using a React Hook that lets you add a state to represent the state of the posts that appear on the paginated table.

To display the returned data, you will use pagination. Pagination is the process of separating content into discrete pages, and it can be used for handling data sets in React. In your application, you’ll render the columns in the paginated table. The columns constant is used to define the table that is present on the webpage.

The useTable hook creates a paginated table. The useTable hook takes in the columns, posts, and setPosts objects as parameters. It returns a paginated table that is assigned to the table constant. The table constant renders the paginated table on the webpage. The return statement returns the paginated table. The useSortBy hook sorts the paginated table by the column headers. The usePagination hook creates the pagination buttons at the bottom of the table that are used to navigate through the paginated table.

Importing the HTTP client

Your application needs a way to communicate with and retrieve resources from RESTful web services to output the resources onto the paginated table. The Axios library will provide you with an HTTP client. This client is used to make HTTP requests to external resources. Axios is a promise-based HTTP client that can send asynchronous requests to REST endpoints. To learn more about the Axios library and its HTTP client, see the Axios documentation.

The GetArtistsInfo() function uses the Axios API to fetch data from your back end. This function is called when the ArtistTable is rendered to the page using the useEffect() React lifecycle method.

Update the ArtistTable.js file.
src/main/frontend/src/Components/ArtistTable.js

ArtistTable.js

link:finish/src/main/frontend/src/Components/ArtistTable.js[role=include]

Add the axios library and the GetArtistsInfo() function.

The axios HTTP call is used to read the artist JSON that contains the data from the sample JSON file in the resources directory. When a response is successful, the state of the system changes by assigning response.data to posts. The artists and their albums JSON data are manipulated to allow them to be accessed by the ReactTable. The …​rest or …​album object spread syntax is designed for simplicity. To learn more about it, see Spread in object literals.

Building and packaging the front end

After you successfully build your components, you need to build the front end and package your application. The Maven process-resources goal generates the Node.js resources, creates the front-end production build, and copies and processes the resources into the destination directory.

In a new command-line session, build the front end by running the following command in the start directory:

mvn process-resources

The build may take a few minutes to complete. You can rebuild the front end at any time with the Maven process-resources goal. Any local changes to your JavaScript and HTML are picked up when you build the front end.

Navigate to the http://localhost:9080 web application root to view the front end of your application.

Testing the React client

New projects that are created with create-react-app comes with a test file called App.test.js, which is included in the src/main/frontend/src directory. The App.test.js file is a simple JavaScript file that tests against the App.js component. There are no explicit test cases that are written for this application. The create-react-app configuration uses Jest as its test runner. To learn more about Jest, go to their documentation on Testing React apps.

App.test.js

link:finish/src/main/frontend/src/App.test.js[role=include]
Update the pom.xml file.
pom.xml

pom.xml

link:finish/pom.xml[role=include]

To run the default test, you can add the testing configuration to the frontend-maven-plugin. Rerun the Maven process-resources goal to rebuild the front end and run the tests.

Although the React application in this guide is simple, when you build more complex React applications, testing becomes a crucial part of your development lifecycle. If you need to write application-oriented test cases, follow the official React testing documentation.

When you are done checking the application root, exit dev mode by pressing CTRL+C in the shell session where you ran the Liberty.

Great work! You’re done!

Nice work! You just accessed a simple RESTful web service and consumed its resources by using ReactJS in Open Liberty.