1

I'm trying to bundle ReactJS and Spring Boot API together and build one fat jar. In every tutorial I read, I'm told to put the localhost API URL as a proxy in package.json of the React app like below.

"proxy": "http://localhost:8080"

As I obviously don't have PROD deployment experience with this, is this the way to go when you are deploying in PROD? Else, please guide me in the right direction. I couldn't find the answer anywhere.

Also, any cons in doing so in a medium sized project with two developers? Appreciate any input.

2
  • did you took a looked at my answer? Commented Feb 15, 2021 at 16:12
  • Yes I just did. Replied to the answer. Commented Feb 15, 2021 at 23:05

1 Answer 1

1

The "proxy" field should only be used in development environment when the Webpack dev server is first in line(to enable the Hot-Reload feature) Here is a guide from 2018: spring + react guide

regardless there are two main way of hosting the react app:

  1. inside the spring boot Jar a static resource(you can use frontend-maven-plugin to run yarn/ npm again see the guide), the advantages of this method is security, you don't need CORS enabled to serve the page. the disadvantages is convenient this solution require more code, also the spring boot server handles UI serving to the client that requires extra calls to the server(spring first approach)
  2. the other option is to host it in a hosting service like amazon S3 and then it will be hosted not in spring but in s3 and will be the first in line(UI first approach), you will need to enable CORS in spring boot app, but this is a more continent solution.

ps. I would read some guides first, it would help you with general understanding

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

5 Comments

Thanks for the descriptive answer Roie. Clears lots of things for me. In the first way of doing it, how would you define the URL for the fetch requests if both the UI and the backend is bundled together in deployment? Also, thanks for the link to the tutorial.
@suharsha because spring is the first in line any controller that you will add will be in priority over the UI, spring first is trying to match path to a controller and only if non is found go to static by default....
I think you did not get my question. If I'm doing a fetch request in my React app to a resource in my Spring Boot backend (which are bundled together), what is the URL for the backend? As an example, will it be localhost/api/get?
don't use the initial part of the URL, this is in the example, but hostname/api/get -> fetch(api/get) should be enough it will resolve the hostname automatically
Oh that makes sense. Thank you so much Roie for the clarifications. Much appreaciated :)

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.