I have been struggling to get my react app running over spring boot server. On running the spring server, I am able to see contents on localhost:8080
however spring boot is not able to serve any static files /about /login etc.
I get the whitelabel Error Page 404
error .
When I run the react app standalone on npm, I am able to route without any problem.
I have followed the tutorials mentioned here but simplified it for my purposes.
The structure of my project looks like
src
main
java
App.java
resources
application.propperties
webapp
src
/components
NavMain.js
/pages
AboutPage.js
WelcomePage.js
/helpers
app.js
This is what my App.JS looks like
<div>
<NavMain />
<BrowserRouter>
<Routes>
<Route exact path="/" element={<WelcomePage />} />
<Route exact path="/about" element={<AboutPage />} />
</Routes>
</BrowserRouter>
</div>
I am serving the react files in spring by copying files into static folder pom.xml
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
${basedir}/target/classes/static
</outputDirectory>
<resources>
<resource>
<directory>
src/main/webapp/build
</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
I tried
HashRouter
instead ofBrowserRouter
but does not work- Add a
@controller
java file to route URL but did not work (React-Router issues when serving React App with Spring Boot)