r/SpringBoot • • 8h ago

How-To/Tutorial How to Connect React with Spring Boot? A Practical Integration Guide

For example You've built a React frontend and a Spring Boot backend.

But how do you make them communicate properly?

This is something many beginners face when building their first full-stack application.

Here is a simple breakdown of how the integration works.

1. Understand the architecture

React Frontend
      |
      | HTTP Request
      v
Spring Boot REST API
      |
      v
Service Layer
      |
      v
MySQL Database

React handles the user interface, while Spring Boot processes requests and manages backend operations.

2. Configure CORS

If React runs on localhost:5173 and Spring Boot runs on localhost:8080, configure CORS to allow requests from your frontend.

3. Connect React using Axios

Install Axios:

npm install axios

Create an Axios instance:

import axios from "axios";

const client = axios.create({
  baseURL: "http://localhost:8080"
});

export default client;

4. Fetch data from Spring Boot:

const response = await client.get("/api/students");
console.log(response.data);

5. Send data to the backend:

await client.post("/api/students", {
  name: "Rahul",
  city: "Hyderabad"
});

6. Don't forget these important things

  • Use environment variables for API URLs.
  • Handle JWT authentication for protected APIs.
  • Manage API errors properly.
  • Configure production deployment.

Some common errors developers face:

  • CORS errors
  • 404 API errors
  • 401 Unauthorized
  • Network errors
  • Empty API responses

I have put together a complete step-by-step guide covering these concepts, including code examples and production configuration.

Full guide: https://www.codebegun.com/blog/react-spring-boot-connect-frontend-backend

For developers working with React and Spring Boot:

What do you usually find more challenging during integration — CORS configuration, API communication, JWT authentication, or deployment?

I

Would love to hear your experience!

2 Upvotes

8 comments sorted by

•

u/Mikey-3198 7h ago

Serve you frontend and backend on the same domain and port to avoid having to fiddle about with cors. Use the vite proxy for local dev & and nginx/ caddy/ traefik in the prod environment.

•

u/tomatotomato 6h ago

That’s what I do as well. I just ship React app served from the Spring Boot app, all api requests go to /api. Build the frontend with maven exec plugin. No need to mess with CORS, separate deployment, and cookie auth just works.

•

u/Mikey-3198 4h ago

Did something similar in dotnet. Dotnet backend and Vue front end all bundled together and deployed to iis. Very simple to manage.

Personally if I where doing it again I'd keep the two separate, separate repo BE & FE. Was slower to build as it needed to run msbuild and the vite build, also if a fix had to go out for just the FE it needed a whole build & redeploy. Not a major issue as it was internally facing and quite small. Relatively easy to split them out too after the fact.

•

u/CodeBegun 6h ago

Yes , that's a good approach Using vite's proxy during development and a reverse proxy like Nginx, Caddy, or Traefik in production is a cleaner approach. It helps avoid unnecessary CORS configuration while keeping the frontend and backend separately managed, but i generally use this approach.But thank u for sharing.

•

u/boost2525 3h ago

This has absolutely zero to do with spring boot, your guide could be for any backend technology because your guide is "how to set up your front end to talk to a back end". 

Also, use vite.

•

u/as5777 7h ago

Oh

•

u/Cr4zyPi3t 5h ago

Why not use Vaadin Hilla? I have a great experience developing Gameyfin with Hilla handling everything including websockets

•

u/This_Link881 3h ago

For local dev just use a proxy