How To Develop and Build React App With Java Backend
Learn How you develop and build 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 NodeJS 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.
In this post, we will see the details and implementation with Java. We will go through step by step with an example.
- Introduction
- Prerequisites
- Example Project
- How To Build and Develop The Project
- How To Build For Production
- Summary
- Conclusion
Introduction
React is a javascript library for building web apps and it doesn’t load itself in the browser. We need some kind of mechanism that loads the index.html (single page) of React with all the dependencies(CSS and js files) in the browser. In this case, we are using java as the webserver which loads React assets and accepts any API calls from the React app.

If you look at the above diagram all the web requests without the /api will go to React router. All the paths that contain /api will be handled by the Apache Tomcat container.
Prerequisites
There are some prerequisites for this article. You need to have java installed on your laptop and how http works. If you want to practice and run this on your laptop you need to have these on your laptop.
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.

As you add users we are making an API call to the Java server to store them and get the same data from the server when we retrieve them. You can see network calls in the following video.

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 3000cd /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 and Develop The Project
Usually, the way you develop and the way you build and run in production are completely different. Thatswhy, I would like to define two phases: Development phase and Production phase.
In the development phase, we run the java server and the React app on completely different ports. It’s easier and faster to develop that way. If you look at the following diagram 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.

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.


If you look at the above project structure, all the React app resides under the src/main/ui folder and Java code resides under the src/main/java folder. All the resources are under the folder /src/main/resources such as properties, static assets, etc
Java API
We use spring boot and a lot of other tools such as Spring Devtools, Spring Actuator, etc under the spring umbrella. Nowadays almost every application has spring boot and it is an open-source Java-based framework used to create a micro Service. It is developed by the Pivotal Team and is used to build stand-alone and production-ready spring applications.
We start with Spring initializr and select all the dependencies and generate the zip file.

Once you import the zip file in the eclipse or any other IDE as a Maven project you can see all the dependencies in the pom.xml. Below is the dependencies section of pom.xml.







