Spring Boot React.js Full Stack Tutorial By Example

This tutorial guides you through building a full-stack task management application using Spring Boot (backend) with Java 21, Open Liberty as the application server, and React.js (frontend). The app supports CRUD operations (Create, Read, Update, Delete) for tasks. We’ll use an H2 in-memory database for simplicity, but you can adapt it to other databases like PostgreSQL. Each code block includes detailed descriptions and inline comments to clarify functionality. Diagrams illustrate the architecture, and external resources are linked for beginners.


Prerequisites


Project Overview

  • Backend: A Spring Boot application with RESTful API endpoints for task management, running on Open Liberty.
  • Frontend: A React.js app that interacts with the backend via HTTP requests.
  • Database: H2 in-memory database (learn more at H2 Database).
  • Features:
    • List all tasks
    • Create a new task
    • Update an existing task
    • Delete a task

Architecture Diagram

Below is a diagram showing the application architecture:

Explanation:

  • The React.js frontend sends HTTP requests (GET, POST, PUT, DELETE) to the Spring Boot backend.
  • The Spring Boot backend processes requests via the TaskController, uses TaskRepository to interact with the H2 database, and returns JSON responses.
  • Open Liberty serves as the application server, hosting the Spring Boot app.
  • The H2 database stores task data in memory during runtime.

For more on full-stack architecture, see Baeldung’s Full-Stack Guide.


Step 1: Set Up the Spring Boot Backend

1.1 Create a Spring Boot Project

  1. Visit Spring Initializr to generate a project with:
    • Project: Maven
    • Language: Java
    • Spring Boot: 3.3.x (latest stable as of April 2025)
    • Java: 21
    • Dependencies:
      • Spring Web (for REST APIs)
      • Spring Data JPA (for database operations)
      • H2 Database (in-memory database)
    • Download and extract the zip file.
  2. Import the project into your IDE.

Why? Spring Initializr simplifies project setup by generating a pre-configured Spring Boot application. For beginners, see Spring Initializr Guide.

1.2 Configure Open Liberty

Open Liberty is a lightweight, open-source application server compatible with Spring Boot. Since Spring Initializr may not directly support Open Liberty, we configure it manually.

Add Open Liberty Dependency: Update pom.xml to include the Open Liberty runtime.xml

[xml]<!– Define dependencies for the project –> <dependencies> <!– Open Liberty runtime for running the Spring Boot app –> <dependency> <groupId>io.openliberty</groupId> <artifactId>openliberty-runtime</artifactId> <version>24.0.0.10</version> <type>pom</type> <scope>provided</scope> <!– Provided by the server, not bundled in the app –> </dependency> <!– Other dependencies (e.g., Spring Web, JPA) are already included by Initializr –> </dependencies>[/xml]

Description: This dependency ensures the app can run on Open Liberty. The provided scope means Open Liberty provides the runtime libraries. Learn more at Open Liberty Documentation.

Configure Liberty Maven Plugin: Add the plugin to pom.xml to manage the Open Liberty server.xml

Description: The liberty-maven-plugin allows you to start, stop, and deploy the app to Open Liberty via Maven commands. See Liberty Maven Plugin Guide.

Create server.xml: In src/main/liberty/config, create server.xml to configure the Open Liberty server.xml

Description: This file configures Open Liberty to support Jakarta EE 10 (for JPA and REST) and Spring Boot 3. It sets the server to listen on port 8080 for HTTP requests. For more, see Open Liberty Configuration.

Enable Java 21: Ensure pom.xml specifies Java 21.xml

Description: This ensures the project compiles and runs with Java 21 features (e.g., records, sealed classes). Learn about Java 21 at Oracle’s Java 21 Guide.

1.3 Create the Task Entity

Create a Task entity in com.example.demo.model to represent a task in the database.

java

Description: This class defines the Task entity, which maps to a database table via JPA annotations. @Entity indicates it’s a database entity, @Id marks the primary key, and @GeneratedValue auto-increments the ID. The fields (id, title, description, completed) represent task attributes. For more on JPA, see Spring Data JPA Guide.

1.4 Create the Repository

Create a TaskRepository interface in com.example.demo.repository to interact with the database.

java

Description: This interface extends JpaRepository, which provides built-in CRUD methods (e.g., findAll(), save(), deleteById()). The type parameters <Task, Long> indicate it manages Task entities with a Long ID. Learn more at Spring Data JPA Docs.

1.5 Create the REST Controller

Create a TaskController in com.example.demo.controller to handle HTTP requests.

java

Description: This controller defines REST endpoints for task management:

  • GET /api/tasks: Fetch all tasks.
  • GET /api/tasks/{id}: Fetch a task by ID.
  • POST /api/tasks: Create a new task.
  • PUT /api/tasks/{id}: Update a task.
  • DELETE /api/tasks/{id}: Delete a task. The @CrossOrigin annotation allows requests from the React frontend. ResponseEntity handles HTTP status codes (e.g., 200 OK, 404 Not Found). For more on REST APIs, see Spring REST Guide.

1.6 Configure Application Properties

Update src/main/resources/application.properties to configure the H2 database and server port.

properties

Description: This file configures the H2 database connection and enables the H2 console for viewing data. The server runs on port 8080 to avoid conflicts with the React frontend (port 3000). Learn more at Spring Boot Properties Guide.

1.7 Run the Backend

Start the Open Liberty server:

bash

The backend will be available at http://localhost:8080. Test the API using Postman or curl:

bash

You can also access the H2 console at http://localhost:8080/h2-console (use the JDBC URL jdbc:h2:mem:testdb).

Why? The liberty:run command starts Open Liberty and deploys the Spring Boot app. For more, see Open Liberty Maven Guide.


Step 2: Set Up the React.js Frontend

2.1 Create a React App

  1. In a terminal, create a new React app:bashnpx create-react-app task-manager-frontend cd task-manager-frontend
  2. Install dependencies for HTTP requests and styling:bashnpm install axios bootstrap

Description: create-react-app sets up a React project with a development server and build tools. axios handles HTTP requests, and bootstrap provides CSS styling. For beginners, see Create React App Docs and Axios Guide.

2.2 Create the Task Component

Create src/components/TaskList.js to display and manage tasks.

jsx

Description: This component:

  • Uses useState to manage tasks and form input state.
  • Uses useEffect to fetch tasks when the component mounts.
  • Defines functions (handleCreate, handleUpdate, handleDelete) to send HTTP requests via Axios.
  • Renders a form to create tasks and a list of tasks with checkboxes and delete buttons. Bootstrap classes (container, form-control, btn) provide styling. For more on React hooks, see React Hooks Guide.

2.3 Update App.js

Update src/App.js to include the TaskList component and Bootstrap CSS.

jsx

Description: This is the root component that imports Bootstrap CSS and renders the TaskList component. For more on React components, see React Components Guide.

2.4 Run the Frontend

Start the React app:

bash

The frontend will be available at http://localhost:3000.

Why? The npm start command runs the React development server, which supports hot reloading for faster development. See Create React App Docs.


Step 3: Test the Application

  1. Ensure the backend (Open Liberty) is running on http://localhost:8080.
  2. Ensure the frontend (React) is running on http://localhost:3000.
  3. Open http://localhost:3000 in your browser.
  4. Test the following:
    • Add a new task by entering a title and description, then clicking “Add Task.”
    • Mark a task as completed by checking its checkbox.
    • Delete a task by clicking its “Delete” button.

The frontend communicates with the backend via REST APIs, and changes are stored in the H2 database (in-memory).

Data Flow Diagram:

Explanation: The frontend sends HTTP requests to the backend, which uses JPA to interact with the H2 database. Responses are sent back as JSON.


Step 4: Deploying the Application (Optional)

To deploy the app:

  1. Backend:
    • Package the Spring Boot app with Open Liberty:bashmvn clean package liberty:package
    • The generated .war file (in target/) can be deployed to an Open Liberty server in production. See Open Liberty Deployment Guide.
  2. Frontend:
    • Build the React app:bashnpm run build
    • Serve the build folder using a static file server like Nginx or bundle it with the backend. See React Deployment Guide.

Conclusion

You’ve built a full-stack task management application using Spring Boot with Java 21, Open Liberty, and React.js. The backend provides a RESTful API, and the frontend offers a user-friendly interface. Each code block was explained with detailed comments to clarify its purpose.

Next Steps

For further learning, explore:

Happy coding! Let me know if you need help with extensions or debugging.

Leave a Comment

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

Scroll to Top