spring-boot-react-crud-example-feature-image

Spring Boot, React, MySQL: CRUD example REST APIs

In this tutorial, we will learn how to build a full stack Spring Boot + React.js + MySQL example with a CRUD App. The back-end server uses Spring Boot with Spring Web MVC for REST APIs and Spring Data JPA for interacting with MySQL/PostgreSQL database. Front-end side is made with React, React Router, Axios & Bootstrap.

Related Posts:
Spring Boot, React & MongoDB example: Build a CRUD Application
Spring Boot + React: Login example with JWT Authentication & Spring Security
React Upload/Download Files to/from Spring Boot Rest Apis


Spring Boot React CRUD example Overview

We will build a full-stack Tutorial Application in that:

  • Each Tutorial has id, title, description, published status.
  • We can create, retrieve, update, delete Tutorials.
  • We can also find Tutorials by title.

The images below shows screenshots of our System.

– Create an item:

spring-boot-react-crud-example-rest-api-demo-create

– Retrieve all items:

spring-boot-react-crud-example-rest-api-demo-retrieve

– Click on Edit button to retrieve an item:

spring-boot-react-crud-example-rest-api-demo-retrieve-one

On this Page, you can:

  • change status to Published using Publish button
  • remove the Tutorial from Database using Delete button
  • update the Tutorial details on Database with Update button

spring-boot-react-crud-example-rest-api-demo-update

– Search items by title:

spring-boot-react-crud-example-rest-api-demo-search

Architecture of Spring Boot React CRUD example

This is the application architecture we’re gonna build:

spring-boot-react-crud-example-rest-api-architecture

– Spring Boot exports REST Apis using Spring Web MVC & interacts with Database using Spring JPA
– React Client sends HTTP Requests and retrieve HTTP Responses using axios, shows data on the components. We also use React Router for navigating to pages.
– Database could be MySQL or PostgreSQL.

You can also find the Spring Restful Apis that works with MongoDB here:
Spring Boot, React & MongoDB example: Build a CRUD Application

Spring Boot Rest Apis Back-end

Overview

These are APIs that Spring Boot App will export:

MethodsUrlsActions
POST/api/tutorialscreate new Tutorial
GET/api/tutorialsretrieve all Tutorials
GET/api/tutorials/:idretrieve a Tutorial by :id
PUT/api/tutorials/:idupdate a Tutorial by :id
DELETE/api/tutorials/:iddelete a Tutorial by :id
DELETE/api/tutorialsdelete all Tutorials
GET/api/tutorials?title=[keyword]find all Tutorials which title contains keyword

– We make CRUD operations & finder methods with Spring Data JPA’s JpaRepository.
– The database could be MySQL or PostgreSQL depending on the way we configure project dependency & datasource.

Technology

  • Java 8
  • Spring Boot 2 (with Spring Web MVC, Spring Data JPA)
  • PostgreSQL/MySQL
  • Maven 3.6.1

Project Structure

spring-boot-react-crud-example-spring-server-project-structure

Tutorial data model class corresponds to entity and table tutorials.
TutorialRepository is an interface that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in TutorialController.
TutorialController is a RestController which has request mapping methods for RESTful requests such as: getAllTutorials, createTutorial, updateTutorial, deleteTutorial, findByPublished
– Configuration for Spring Datasource, JPA & Hibernate in application.properties.
pom.xml contains dependencies for Spring Boot and MySQL/PostgreSQL.

Implementation

You can find step by step to implement this Spring Boot Server Application in the post:
Spring Boot, Spring Data JPA – Building Rest CRUD API example

React.js Front-end

Overview

spring-boot-react-crud-example-react-components-diagram

– The App component is a container with React Router. It has navbar that links to routes paths.

TutorialsList component gets and displays Tutorials.
Tutorial component has form for editing Tutorial’s details based on :id.
AddTutorial component has form for submission new Tutorial.

– These Components call TutorialDataService methods which use axios to make HTTP requests and receive responses.

Technology

  • React 16
  • react-router-dom 5.1.2
  • axios 0.19.2
  • bootstrap 4.4.1

Project Structure

spring-boot-react-crud-example-react-client-project-structure

package.json contains 4 main modules: react, react-router-dom, axios & bootstrap.
App is the container that has Router & navbar.
– There are 3 components: TutorialsList, Tutorial, AddTutorial.
http-common.js initializes axios with HTTP base Url and headers.
TutorialDataService has methods for sending HTTP requests to the Apis.
.env configures port for this React CRUD App.

Implementation

You can find step by step to implement this React App in the post:
React.js CRUD example to consume Web API
– or React Hooks CRUD example to consume Web API

Conclusion

Now we have an overview of Spring Boot React CRUD example when building a CRUD App that interact with MySQL/PostgreSQL database.

We also take a look at client-server architecture for REST API using Spring Web MVC & Spring Data JPA, as well as React project structure for building a front-end app to make HTTP requests and consume responses.

Next tutorials show you more details about how to implement the system (including source code):
Back-end
– Front-end:

With Pagination:
React Pagination with API using Material-UI

react-pagination-with-api-material-ui-change-page

Happy learning, see you again!

Leave a Reply

Your email address will not be published. Required fields are marked *