top of page

How To Create Fullstack React App + Spring Rest API Integration

  • Writer: Anand Nerurkar
    Anand Nerurkar
  • Jul 29, 2023
  • 4 min read

Updated: Sep 7, 2023

What we will build

We will build a full-stack web application - basic Employee Management Application with CRUD features:

  • Create Employee

  • List Employee

  • Update Employee

  • Delete Employee

Quick overview of React and Spring Boot


What is React JS

  • React is used to build user interfaces (UI) on the front end.

  • React is not a framework (unlike Angular, which is more opinionated).

  • React is an open-source project created by Facebook.

What is Spring Boot

  • Spring boot to develop REST web services and microservices.

  • Spring Boot has taken the Spring framework to the next level. It has drastically reduced the configuration and setup time required for spring projects.

  • You can set up a project with almost zero configuration and start building the things that actually matter to your application.


Source Code GitHub


Assumption:

Node JS ,NPM installed

Bootstrap installed

React Snippet is installed in VS code

ree

React + Springboot Rest API -Architecture

ree

To create React App

Execute command -

npx create-react-app react-frontend

This command will create react application layout with all required dependency and library.


React Application Structure


ree

package.json


ree

node_modules

This folder is created when you create react app. it contains all dependent modules


public/index.html

This is SPA, so only index.html fill will be served when we start application. This index.html file contain div element with id as root. This will load App.js ->this js file make use of ReactDOM.render method to render App. This App is then then rendered in root element of index.html.

ree

src:

ree

src/App.js

function based (JSX) react component. App.js is root component which is rendered in index.html

ree

To run app

npm start


hit http://localhost:3000 in browser

ree

Edit App.js, remove all parts from header section, simply put hello world

ree

This will refresh page

ree

To integrate bootstrap4

There are 2 ways we can integrate

  1. using bottstrap cdn link

ree

copy above url and paste it in index.html -header -title section as below

ree


2. npm install bootstrap --save

This will add entry into package.json


Before adding entry into package.json


ree

After adding entry into package.json

ree

import bootstrap into index.js

ree

To create List Employee React Component


Goto src folder-create folder component - create ListEmployeeComponent.jsx

Inside ListEmployeeComponent.jsx, we will make use of React Snippet utility. For this, simple type rcc and enter , it will create template for this file as below


ree

ree

ree

ree

Edit app.js as below


ree



ree

To integrate REST API call into React


we will use AXIOS http library to give a call to rest api endpoint


For this , we need to first install axios library to react app

npm install axios --save --- this will add entry into package.json

ree

goto src/create services folder, create EmployeeService.js

services/EmployeeService.js


ree

Goto ListEmplpoyeeComponent.jsx and use componenet life cycle method componentDidMount()

ree
ree

create component/HeaderComponent.jsx as below

ree

update render method as below

ree

create component/FooterComponent.jsx

ree

update render method

ree

Edit App.js and inclide header and footer component


ree


ree

Configure Routing

Install React Router component with below command

npm install react-router-dom

Configure routing in App.js

App.js

use below import

ree

we are importing browserrouter as router,route ,switch element from react-router-dom.switch element is used to ensure that one component render at a time.

include router tag in function App as below

ree

define route path within switch as below

ree

let us test route as below

hit http://localhost:3000/ or http://localhost:3000/employees in browser, router should take to listing page

ree


ree

Adding Add Employee route

create component/AddEmployeeComponent.jsx

ree

Goto App.js and router for AddEmployee fucntion.This router object maintain history object for all routes defined.This is being passed to props obejct. so we make use of this.props.history.push('router path') to route to that component.

ree

Edit ListEmployeeComponent to add a button for Add employee as below

ree

Add eventhandling for Add Employee as below

this.addEmployee=this.addEmployee.bind(this);

ree

In addEmployee(), we will goto add employee form, so we need to make use of routing as below


ree

Since we have configured route in App.js, React router maintain a history object, that keeps mapping for all routing. React Router passes history objecy to each route as props. With this propes we can get access to history object and control routing mapping based on flow.

so we make use of this.props.history.push('/add-employee');


update createEmployeeComponent.jsx as below


ree


ree

Click Add Employee,employee form is displayed

ree

Add employee Form


ree


ree

define properties in state so that we can capture values


ree

ree

Adding eventhandler for firstname as below


ree
ree

ree

bind this eventhandling for save into contrcutor


ree

ree


ree

ree


ree

click on cancel button - will take you to listing page


ree

clikc on save, see console



ree

Integrate rest endpoint for adding employee to DB

Goto EmployeeService.js and add call (createEmployee(employee) for adding employee to DB as below

ree

Update CreateEmployeeComponent.jsx/saveEmployee method for adding rest api call for adding employee info to DB and then route it back to listing as below

ree


ree

ree

create UpdateEmployeeComponent.jsx as below


ree

update App.js for routing info to update component


ree

Add update button ot listing page for updating employee as below

ree

ree

ree

update service class to get a employee based on id as below



ree

update employee page is same as create employee page,just update saveemployee with updateemployee and add updateemployee method.


ree

update lifecycle method componentDId() as below


ree

ree


ree

update data with update api


edit EmployeeService.js with update api call for restendpoint as below

ree

Edit UpdateEmployeeComponent.jsx and include method updateEmployee as below

to integrate updateapi call.

ree

click

ree

click save and data is updated to db with api call

Ramesh is updated to Ram

ree

Delete Data with delete api


Edit listing page and add delete button , bind eventhandler as below


ree

ree

add deleteemployee method as below to call rest api to delete records and filter deleted employee from employee array for performance rather than navigating to listing page again as below.

ree

ree

update EmployeeService.js with delete method as below


ree

ree

click on delete for employee amir


ree

Conclusion

We have successfully created React Application - Employee Management and integrated with Employee Resource Endpoint.



 
 
 

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
  • Facebook
  • Twitter
  • LinkedIn

©2024 by AeeroTech. Proudly created with Wix.com

bottom of page