React + Spring Boot Full Stack Example
Project Overview
Goal: Display a list of users fetched from backend
Frontend: React (calls API)
Backend: Spring Boot (serves JSON)
Backend: Spring Boot (Java)
1. User.java (Entity Class)
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
public int getId() { return id; }
public String getName() { return name; }
2. UserController.java
@RestController
@CrossOrigin(origins = "http://localhost:3000")
public class UserController {
@GetMapping("/users")
public List<User> getUsers() {
return Arrays.asList(
new User(1, "Bala"),
new User(2, "Ajay"),
React + Spring Boot Full Stack Example
new User(3, "Divya")
);
Frontend: React
1. React Component (App.js)
import React, { useEffect, useState } from "react";
function App() {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch("http://localhost:8080/users")
.then((res) => res.json())
.then((data) => setUsers(data));
}, []);
return (
<div>
<h2>User List</h2>
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</div>
);
}
React + Spring Boot Full Stack Example
export default App;
Result
Spring Boot sends user data (JSON)
React fetches it and displays on browser