How to Package Your React App with Spring Boot

Find out how to combine Create React App with Spring Boot to give you a single war file

Emmanuella Aninye
Analytics Vidhya
4 min readMar 2, 2020

--

A little backstory on how this started. My team needed to save some on money on the infrastructure we were requesting and since most of the load for the application we wanted to build would be on the client-side rather than the service side. We decided to see if we could combine a Spring Application with a React app and serve up a single war file.

The basic idea of what Spring Boot and Create React App do.

  1. Create React App helps you start a React project very quickly. It gives you all the basic things you need to get up and running asap.
  2. Spring boot helps you start and maintain a spring application quickly and easily.

Goals:

  • Frontend and backend in a single war file with optimized production builds
  • Keeping the benefits Create React App gives such as hot reloading

Setup:

  • Must have java installed. Head over here to download a version
  • Must have maven installed. For Mac, I used Homebrew (brew install maven )but otherwise, head here
  • Must have node installed. For Mac, I used Homebrew (brew install node )but otherwise, head over here

Side note: my IDE of choice is IntelliJ. When working on react code I usually switch over to VS Code. Feel free to use what makes you feel comfortable

  1. Create an empty repo on Github and add a Read Me, gitignore, license, etc.
  2. Head over to https://start.spring.io to create your spring application and download locally. Group and Artifact can also be anything you want it to be.

GroupId: e.the.awesome

Artifact: spring-react-combo-app

3. Unzip the downloaded project into your git directory. Commit, Commit, Commit. Your SpringReactComboAppApplication should look like this.

4. Let’s create a basic service now. We’ll call it the DadJokesController. This should be created in the same folder as SpringReactComboAppApplication file. I’m aware this is not a proper Rest API format but let’s ignore that for now.

5. In your terminal run

mvn spring-boot:run

Then in your browser check http://localhost:8080/api/dadjokes . You should see the dad joke we added to our controller.

6. To create your React app, just run in the root directory

npx create-react-app basic-frontend-app

You can call it whatever you want, I just called mine basic-frontend-app

7. To run the front end application:

cd basic-frontend-app
npm start

After starting it should look like:

8. Since we want to integrate our Dad Jokes service into the frontend, first we will address proxy issues. If you’ve noticed, your service starts on localhost:8080 while the frontend starts on localhost:3000. If we try calling our service from the frontend, depending on your browser, you’ll get a CORS error.

The simplest way to address the issue is to have your front end proxy any requests from port 3000 to 8080. This change will be made in your package.json file

Add the following to your frontend App.js file

Restart the front end and you should be good. if you happen to get this error: I deleted my package-lock.json file and node_modules folder reinstalled npm packages and ran it again

9. Your application should now look like. You can see the results of the dad jokes API call.

10. Now that our basic front end and backend is complete, it’s time to create a production build and single war file.

Under the <dependencies> add this in

Under the <plugins> section of the pom file, we will add the following commands which will do the following things when mvn clean install is run.

  • npm install with the specified version of node
  • run a production build of our frontend
  • deposit the production build

Side note: order is important for your plugins so make sure your node/npm install execution is before copying the build file execution

11. After adding this, run mvn clean install and verify that the target/classes directory contains both frontend files and java files. And you should be good to go.

A final look at my pom file.

So that’s all I got. If you would like to take a look at the repo or use it. It can be found here on my Github.

Next up is an article on how to deploy your war file on Heroku. Look forward to it!

--

--

Emmanuella Aninye
Analytics Vidhya

Software engineer @Netflix, worker of side projects, and avid reader. I like to get things done. emmanuella.tech/