2

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?

2

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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...

1 Comment

Thanks, I'll take a look at the links. But just to clarify, my issue is in understanding how to configure things so it appears as one integrated app. I know how to start up the client and then browse to localhost:3000. But I want to be able to browse to localhost:8080 and see my react pages (rendered by app.js). I just read about the `frontend-maven-plugin'. Not sure if that's the right way to go, but I'm going to play with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.