How To Build React with Java Backend For Production
A step by step guide with an example project
There are so many ways we can build React apps and ship for production. One way is to build React with or Java and another way is to build the React and serve that static content with NGINX web server. With Java we have to deal with the server code as well, for example, you need to load index.html page with java controller.
In this post, we will see the details about building a React app with Java Backend for production. We will go through step by step with an example.
- Prerequisites
- Example Project
- How To Build For Production
- Summary
- Conclusion
Prerequisites
I have written an article on how to build and develop a React app with Java Backend. If you are not familiar with it and go through the whole process here is the article.
How To Develop and Build React App With Java Backend
Example Project
This is a simple project which demonstrates developing and running React application with Java. We have a simple app in which we can add users, count, and display them at the side, and retrieve them whenever you want.

Here is a Github link to this project. You can clone it and run it on your machine.
// clone the project
git clone https://github.com/bbachi/react-java-example.git// Run React on port 3000
cd /src/main/ui
npm install
npm start// Run Java Code on 8080
mvn clean install
java -jar target/users-0.0.2-SNAPSHOT.jarHow To Build For Production
Usually, the way you develop and the way you build and run in production are completely different. As you have seen in the prerequisite project, in the development phase, we run the java server and the React app on completely different ports. The React app is running on port 3000 with the help of a webpack dev server and the java server is running on port 8080.
But in the production, we package the whole application into a war file and deploy that on the tomcat container which usually runs on the port 8080.
Project Structure
Let’s understand the project structure for this project. We need to have two completely different folders for java and react. It’s always best practice to have completely different folders for each one. In this way, you will have a clean architecture or any other problems regarding merging any files.


The only thing that is different between development and production phases is that all the compilation React code goes to this folder /src/main/resources/static. Whatever static assets you put under the static folder you can directly redirect from your Java Controller as below.







