React Form Integration with Spring Boot REST API

Introduction:

In modern web development, React is commonly used for frontend applications, while Spring Boot is widely used for backend REST APIs. In this tutorial, we will learn how to integrate a React form with a Spring Boot backend and send form data using REST API calls.

This guide is beginner-friendly and helps understand frontend-backend communication in Full Stack Development.


Technologies Used :

Frontend:

  • React.js

  • HTML5

  • CSS3

  • JavaScript

  • Axios

Backend:

  • Java Spring Boot

  • REST API

  • PostgreSQL


Create Spring Boot Backend

First, create a Spring Boot project using Spring Initializr.

Required Dependencies

  • Spring Web

  • Spring Data JPA

  • PostgreSQL Driver

  • Lombok


Create Entity Class

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;

    // getters and setters
}

Create Repository

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

}

Create REST Controller

@RestController
@RequestMapping("/api/employees")
@CrossOrigin(origins = "http://localhost:3000")
public class EmployeeController {

    @Autowired
    private EmployeeRepository repository;

    @PostMapping
    public Employee saveEmployee(@RequestBody Employee employee) {
        return repository.save(employee);
    }
}

Create React Frontend

npx create-react-app react-form-app

Install Axios:

npm install axios

Create React Form Component

import React, { useState } from 'react';
import axios from 'axios';

function EmployeeForm() {

  const [employee, setEmployee] = useState({
    name: '',
    email: ''
  });

  const handleChange = (e) => {
    setEmployee({
      ...employee,
      [e.target.name]: e.target.value
    });
  };

  const handleSubmit = async (e) => {
    e.preventDefault();

    await axios.post(
      'http://localhost:8080/api/employees',
      employee
    );

    alert("Employee Saved Successfully");
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        name="name"
        placeholder="Enter Name"
        onChange={handleChange}
      />

      <input
        type="email"
        name="email"
        placeholder="Enter Email"
        onChange={handleChange}
      />

      <button type="submit">
        Save
      </button>
    </form>
  );
}

export default EmployeeForm;

Run the Application :

Start Spring Boot Backend

mvn spring-boot:run

Start React Frontend

npm start

Frontend:

http://localhost:3000

Backend:

http://localhost:8080

Output :

When the user enters details in the React form and clicks the Save button, the data will be stored in the PostgreSQL database through the Spring Boot REST API.


Conclusion :

In this tutorial, we successfully integrated a React frontend form with a Spring Boot backend REST API. This is one of the most important concepts in Full Stack Java Development.


For More: https://sulochanabasavala.blogspot.com/

Next Article : https://sulochanabasavala.blogspot.com/2026/05/jwt-authentication-using-react-and.html

Tags:

React.js, Spring Boot, REST API, PostgreSQL, Full Stack Development, Java, React Form Integration

Happy Learning....

Comments

Popular posts from this blog

React Basics – How to Create Component

How to Build REST API using Spring Boot (Step-by-Step Guide)

Spring Boot @RequestParam Annotation Explained with Real-Time Examples