Java and React: A Practical Full-Stack Guide for 2026
Learn to pair a Java backend with a React frontend using REST/JSON. This practical guide covers API design, DTO mapping, CORS, security, testing, and deployment for a scalable full-stack app.

Java React refers to building a Java backend (often Spring Boot) and a React frontend that communicate via REST/JSON. This guide demonstrates setup, data flow, authentication, and deployment patterns for a robust full-stack app. You'll learn API design, DTO mapping, CORS configuration, testing strategies, and common pitfalls. The java react pattern emphasizes clear separation of concerns and scalable architecture.
Architecture overview: Java backend with React frontend
The java react stack pairs a robust Java backend (commonly Spring Boot) with a dynamic React frontend. In this pattern, the frontend renders UI components and calls a REST API hosted by the Java server. Data travels as JSON, keeping a clear separation between server-side logic and client-side rendering. This separation enables independent scaling, easier testing, and parallel work streams for frontend and backend teams. According to JavaScripting, the java react pattern is gaining momentum in modern web apps due to its strong typing in Java and rich user interfaces in React.
Code samples show a minimal REST controller and a React fetch call to illustrate the end-to-end data flow:
@RestController
@RequestMapping("/api")
public class UserController {
private final UserService service;
public UserController(UserService service) { this.service = service; }
@GetMapping("/users")
public List<UserDto> getUsers() {
return service.findAll();
}
}public class UserDto {
private Long id;
private String name;
private String email;
// getters/setters omitted for brevity
}import React, { useEffect, useState } from 'react';
export default function UserList() {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch('/api/users')
.then(r => r.json())
.then(setUsers)
.catch(console.error);
}, []);
return (
<ul>
{users.map(u => (
<li key={u.id}>{u.name} — {u.email}</li>
))}
</ul>
);
}- The block above demonstrates the core data flow: React renders UI while Java consumes and returns JSON data.
- Variations include using GraphQL for complex queries, or adding pagination headers to optimize bandwidth.
- For production, consider a lightweight API gateway layer to centralize authentication and routing.
wordCount
Steps
Estimated time: 2-3 hours
- 1
Scaffold backend (Spring Boot)
Create a new Spring Boot project (web starter) and define a simple UserController that exposes a GET /api/users endpoint. This establishes the Java side of the API surface that the React app will consume.
Tip: Keep controllers small and focused; prefer service-layer abstractions for testability. - 2
Define DTOs and service layer
Create a UserDto and a UserService that returns sample data. Use Jackson annotations only where needed to ensure stable JSON shapes that align with your React interface.
Tip: Decouple domain models from DTOs to minimize breaking changes when the API evolves. - 3
Create React app and fetch API
Initialize a React app, implement a UserList component that fetches /api/users, and display the data. Add error handling and loading states to improve UX.
Tip: Use a clear error boundary in React to avoid crashing the UI on API failures. - 4
Enable CORS and test locally
Configure CORS on the Spring Boot side or set up a dev proxy in the React app. Verify end-to-end by running both servers and inspecting network calls.
Tip: Prefer a targeted CORS policy during development; tighten it for production. - 5
Run end-to-end and iterate
Start both servers, test with real data, and refine DTOs, error messages, and UI states. Add basic unit tests for the backend and frontend components.
Tip: Automate a basic integration test that calls the API and verifies UI rendering.
Prerequisites
Required
- Required
- Required
- Required
- Basic understanding of REST and JSONRequired
Optional
- Optional
Commands
| Action | Command |
|---|---|
| Run Spring Boot APIFrom backend project directory | mvn spring-boot:run |
| Run React dev serverFrom frontend directory | npm start |
| Build backendBackend project directory | mvn clean package |
| Build frontend for productionFrontend project directory | npm run build |
Questions & Answers
What is the typical Java-React stack for modern web apps?
The typical Java-React stack pairs a Java backend (often Spring Boot) with a React frontend. The frontend consumes REST/JSON endpoints exposed by the backend, enabling a clean separation of concerns and scalable development. This article focuses on practical integration patterns.
The usual setup pairs a Spring Boot backend with a React frontend that talks through REST endpoints.
How do I enable CORS for a React app talking to Spring Boot?
Enable CORS on the backend or via a proxy during development. In Spring Boot, use @CrossOrigin on controllers or a global CorsConfigurationSource. In the frontend, configure a proxy to avoid cross-origin issues in development.
Turn on CORS on the backend or use a dev proxy so your React app can call the Java API without cross-origin errors.
REST vs GraphQL for Java and React?
REST is straightforward and widely supported; GraphQL adds flexibility but requires more setup. For most CRUD apps, REST with Spring Boot and React is simpler to start. Choose GraphQL when you need complex queries or optimized data fetching.
REST is the default choice for most Java-React apps, but GraphQL can help when you need precise data fetching.
How do I run both backend and frontend in development?
Run the Spring Boot API and the React dev server in parallel. Use a proxy (or CORS) to connect them, and consider using npm scripts to start both with a single command.
Start both the backend and the frontend at the same time, using a proxy to connect them.
What are common pitfalls when integrating Java and React?
Common issues include CORS errors, mismatched JSON shapes, authentication token handling, and build-time misconfigurations. Testing both layers together helps catch issues early.
Watch out for CORS, JSON shape mismatches, and token handling when integrating Java backends with React frontends.
What tooling helps with debugging Java-React apps?
Use browser devtools for frontend errors and server-side logging for Java. Tools like Postman/Insomnia test APIs; rely on Spring Boot Actuator for runtime info.
Use browser console and API testers for frontend; check server logs and tools like Actuator for the backend.
What to Remember
- Design clean REST endpoints that align with UI needs
- Use DTOs to decouple backend and frontend contracts
- Configure CORS or a dev proxy to enable local development
- Test end-to-end to catch integration issues early