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?
-
Does this answer your question? Server side rendering React in Java Springbootdeepakchethan– deepakchethan2021-10-10 04:11:29 +00:00Commented Oct 10, 2021 at 4:11
-
This is the best resource I've found for integrating SpringBoot for react dev.to/arpan_banerjee7/…Peter Kronenberg– Peter Kronenberg2021-10-13 02:08:53 +00:00Commented Oct 13, 2021 at 2:08
2 Answers
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
@RestController
@CrossOrigin("http://localhost:3000")
@RequestMapping("/api/v1")
public class UserController {
https://github.com/prashanthbilla/Spring-boot_ReactJs_UserManagement_Application
Comments
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
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.