title: How to serve React built files to Spring Boot application
body: <p>I have a Spring Boot application that has multiple APIs and a standart React application that was created using the <strong><em>create-react-app</em></strong> command. What I want to do is to build the React app using </p>
  
  <blockquote>
    <p>npm run build</p>
  </blockquote>
  
  <p>and serve the built files from the Spring Boot application.</p>
  
  <p>Here is my project structure:</p>
  
  <p><a href="https://i.stack.imgur.com/pwhQB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pwhQB.png" alt="enter image description here"></a></p>
  
  <p>Right now, during development I set a proxy to the frontend/package.json in order to send all requests to the backend server but need the same behaviour in production. How do I achieve that?</p>
  
  <p>package.json:</p>
  
  <pre><code>"proxy": "http://localhost:8082/gui",
  </code></pre>
  
  <p>I also need everything packaged together in my war so in my pom.xml I use a couple of plugins to build the React app and copy the built files to the Spring Boot target folder: </p>
  
  <pre><code>        &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;com.github.eirslett&lt;/groupId&gt;
                  &lt;artifactId&gt;frontend-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.6&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;workingDirectory&gt;frontend&lt;/workingDirectory&gt;
                      &lt;installDirectory&gt;target&lt;/installDirectory&gt;
                  &lt;/configuration&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;install node and npm&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;install-node-and-npm&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;nodeVersion&gt;v12.13.1&lt;/nodeVersion&gt;
                              &lt;npmVersion&gt;6.12.1&lt;/npmVersion&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                      &lt;execution&gt;
                          &lt;id&gt;npm install&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;npm&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;arguments&gt;install&lt;/arguments&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                      &lt;execution&gt;
                          &lt;id&gt;npm run build&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;npm&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;arguments&gt;run build&lt;/arguments&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;generate-resources&lt;/phase&gt;
                          &lt;configuration&gt;
                              &lt;target&gt;
                                  &lt;copy todir="${project.build.directory}/classes/public"&gt;
                                      &lt;fileset dir="${project.basedir}/frontend/build" /&gt;
                                  &lt;/copy&gt;
                              &lt;/target&gt;
                          &lt;/configuration&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;run&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
  </code></pre>
  
  <p>An important information is that the Spring Boot app has a custom context path:</p>
  
  <pre><code>server.servlet.context-path=/gui
  </code></pre>
  
  <p>so the homepage should be served from localhost:8080/gui.
  I need to know how to serve the frontend from my Spring Boot server. Thank you in advance!</p>