28 September 2016

Load Data from Database using REST services in ReactJS or React.js

Hi Friends,

In this post I am going to show you that how you can get the data from databse using REST services and show it on the UI with the help of react JS/ReactJS/React.js.

I am assuming you must be knowing that how to create the Restful webservice to get the data from database table. In my example I have taken "Department" table from HR schema in Oracle database.

If you need any help regarding creating the REST webservice you can refer the following link :


I am assuming that you have already created the rest webservice and deployed on the server.

In my case it is deployed on local weblogic server with the following URL:


You can test your rest service using rest client in firefox as shown below:

So here I can see that I am getting the array of JSON data as the response from the restful webservice.

Now we need to consume it and show it on the UI with help of the ReactJS.

If you are beginner and need to set up the ReactJS environment then you can follow the given URL:


Once you are done with the setup you can copy and paste the dependent file in your project and then you need to modify only your jsx file.

I have done the same and copied all the file from my previous application and now going to modify the jsx file only.

So now let's go and modify the entire code in index.jsx file as follows:

import React from "react";
import ReactDOM from "react-dom";

var DepartmentGridTable = React.createClass({

 getInitialState: function() {
    return {departments: []};
componentDidMount: function() {
      url: this.props.dataUrl,
      dataType: 'json',
      success: function(data) {
        this.setState({departments: data});
      error: function(xhr, status, err) {
        console.error(this.props.dataUrl, status, err.toString());
 render: function(){
         var x = this.state.departments.map(function(d, index){
             return <tr><td>{index+1}</td><td>{d.departmentId}</td><td>{d.departmentName}</td></tr>
        return (
                <h4>Department Data</h4>
                <table id="mytable">
                            <td>Serial Number</td>
                            <td>Department Id</td>
                            <td>Department Name</td>


 <DepartmentGridTable dataUrl ="http://localhost:9001/TestRestEasy/rest/departments/"/>,

 and you index.html file should look like this.

<!DOCTYPE html>

  <title>Hello React</title>

  <div id="container"></div>

  <script src="myCode.js"></script>


Now run your application by running following command 

npm start 

This will start your webpack dev server.

To compile the code run the following command:


Now go to browser and open index.html file with following URL:


You can see the following result on the UI.

0 on: "Load Data from Database using REST services in ReactJS or React.js"