1

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?

Peter Kronenberg
  • 878
  • 10
  • 32
  • Does this answer your question? [Server side rendering React in Java Springboot](https://stackoverflow.com/questions/53652083/server-side-rendering-react-in-java-springboot) – deepakchethan Oct 10 '21 at 04:11

3 Answers3

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

  • 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. – Peter Kronenberg Oct 10 '21 at 14:47
2

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

1

This is the best resource I've found for integrating SpringBoot for react https://dev.to/arpan_banerjee7/run-react-frontend-and-springboot-backend-on-the-same-port-and-package-them-as-a-single-artifact-14pa

Peter Kronenberg
  • 878
  • 10
  • 32