Peter Kronenberg
Peter Kronenberg

Reputation: 1306

How to integrate Spring Boot with React

I'm new to React and having a hard time understand how to integrate React into a Spring Boot server? I used to the old model where both front-end was just part of the server.
Spring is running at localhost:8080. React runs at localhost:3000. The server has rest endpoints that React will want to call.
How do I get Spring to serve index.html, which contains the React component?

Upvotes: 2

Views: 16519

Answers (2)

Prashanth Billa
Prashanth Billa

Reputation: 113

I think you should try this code in your spring-boot application Controller,

You should use @CrossOrigin to allow your React App server in Spring-boot App

enter image description here

@RestController
@CrossOrigin("http://localhost:3000")
@RequestMapping("/api/v1")
public class UserController {

https://github.com/prashanthbilla/Spring-boot_ReactJs_UserManagement_Application

Upvotes: 4

sina adibfar
sina adibfar

Reputation: 109

I did not understand your question exactly, but I will explain these two methods to you, your problem will probably be solved

Answer 1: You can create a folder called public or static inside the resources folder and then inside the resources folder and take a build from the react project and put it in the public or static folder. As soon as you run spring boot, you can run your project at http: // localhost: port See that instead of the port you have to write your port number (for example 8080).

Answer 2: You can fetch data inside react using the api you wrote in spring. This is a simple task and I will give you a tutorial according to which you can do this

https://www.geeksforgeeks.org/how-to-fetch-data-from-an-api-in-reactjs/

I hope your problem is solved with one of these solutions

have a nice day...

Upvotes: 3

Related Questions