Quick revision
The short answer
A refresh on /orders/42 asks the server for that path, and Spring Boot finds neither a static file nor a controller for it, so it returns 404; the route only exists inside the React router in the browser. The fix is to forward unknown paths that are not API routes and do not look like files to index.html, either with a controller mapping such as @GetMapping("/{path:[^\\.]*}") returning "forward:/index.html" or with a resource resolver that falls back to index.html, while excluding /api/** and /actuator/** so API errors stay JSON. We also set no-cache on index.html, long immutable caching on hashed assets, and build the frontend as part of the Maven, Gradle or Docker build.
What to remember
In this answer, we saw that a deep link like /orders/42 returns 404 on refresh because the server has neither a file nor a controller for a route that exists only in the browser. We fixed it by forwarding paths without a dot to index.html, first with a controller using @GetMapping("/{path:[^\\.]*}") style patterns and then with a PathResourceResolver that falls back to index.html at any depth, while excluding /api/** and /actuator/** so API 404s stay JSON. We set no-cache on index.html and long immutable caching on hashed assets, and built the frontend with frontend-maven-plugin or a Docker build stage.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A refresh on /orders/42 asks the server for that path, and Spring Boot finds neither a static file nor a controller for it, so it returns 404; the route only exists inside the React router in the browser. The fix is to forward unknown paths that are not API routes and do not look like files to index.html, either with a controller mapping such as @GetMapping("/{path:[^\\.]*}") returning "forward:/index.html" or with a resource resolver that falls back to index.html, while excluding /api/** and /actuator/** so API errors stay JSON. We also set no-cache on index.html, long immutable caching on hashed assets, and build the frontend as part of the Maven, Gradle or Docker build.
Why this question is asked
Serving a single-page app from the backend jar is a common, simple setup, and this bug appears on the first day someone bookmarks a page or presses F5. Interviewers like it because the fix looks like one line, yet a careless version breaks API error handling or serves HTML in place of a missing JavaScript file. In this answer, we will see why the 404 happens, fix it with a forwarding controller, handle nested paths with a resource resolver, keep API routes untouched, set the right cache headers, and wire the frontend into the build.
1Why the Deep Link Returns 404
A single-page app, or SPA, is one HTML page, index.html, plus JavaScript bundles. When the user clicks a link to /orders/42 inside the app, the React router changes the address bar with the browser history API and renders the order screen without asking the server for anything. So navigation inside the app works.
A refresh, a bookmark or a shared link is different. The browser sends GET /orders/42 to the server. Spring Boot first looks for a controller mapped to that path and finds none, then its static resource handler looks for a file called orders/42 under classpath:/static and finds none, so it answers 404. The server has no idea that /orders/42 is a route of the frontend. What it should do is send index.html, so the React app starts and its router renders the order screen for that URL.
- Path starts with /api or /actuator?YesControllers handle it; unknown paths get a JSON 404NoNext check
- A real static file exists?YesServe the fileNoNext check
- Last segment has a dot, like main.js?YesReturn 404, a missing assetNoForward to index.html
3 steps in order. Play them, or step through them one at a time.
Only paths that are neither API routes nor files fall back to the SPA.
2Fix 1: A Forwarding Controller
The simplest fix is a controller that catches paths without a dot and forwards them, on the server side, to index.html. A forward is internal: the browser keeps the URL /orders/42 and receives the content of index.html.
Notice two details. The regular expression [^\.]* rejects segments containing a dot, so requests for files such as /favicon.ico or a missing /assets/old.js are not turned into HTML; a browser that asks for JavaScript and receives HTML fails with a confusing error. And the negative lookahead (?!api$|actuator$) on the first segment makes sure a request to an unknown API path does not fall into the SPA. Without it, GET /api/nothing would return index.html with status 200, and an API client would try to parse HTML as JSON.
3Fix 2: A Resource Resolver That Falls Back to index.html
Listing one pattern per depth gets clumsy for deep routes. With Spring's default PathPatternParser, a ** wildcard is only allowed at the end of a pattern, so we cannot simply write a pattern for any depth that still checks the last segment. A resource resolver handles any depth in one place. Spring Boot uses its own resource handler for /**, but if we register one for /** ourselves, Boot leaves it to us.
Resource handlers run only after no controller matched, so real API endpoints are never affected. For an unknown /api path the resolver returns null, Spring reports that no resource was found, and Spring Boot's error handling produces its usual 404, as JSON for API clients. The same code handles /orders/42/items/7 and any other depth.
4Getting the Cache Headers Right
Caching decides whether users see a consistent version after a deploy. Build tools such as Vite put a content hash in every asset name, for example index-3f9a1c.js. Because a changed file gets a new name, these files can be cached for a year and marked immutable, so browsers never even revalidate them.
index.html is the opposite. It is the one file whose name never changes, and it points to the current hashed assets. If a browser caches it, users keep loading old bundles after a deploy, or a mix of old HTML and new files that no longer exist. So index.html gets Cache-Control: no-cache, which means the browser must check with the server each time, a cheap 304 when nothing changed. The configuration above gives both.
5Building the Frontend Into the Jar
The jar only contains the SPA if the build puts it in src/main/resources/static or straight into target/classes/static. A common choice with Maven is frontend-maven-plugin, which installs a pinned Node version, runs npm ci and npm run build, and then a resources step copies the dist folder into static. Gradle has equivalent node plugins. Alternatively, a multi-stage Docker build compiles the frontend in a Node image and copies the output into the Java build stage, which keeps Node out of the Java build entirely. Either way, the built files should not be committed to the repository.
6Follow-up questions
How would you handle nested paths like /orders/42/items?
With the forwarding controller, by adding a pattern for each extra depth, which works but has to be updated when the frontend adds deeper routes. The resource resolver in Fix 2 handles any depth automatically: any path that is not an API path, not an existing file and has no dot falls back to index.html. For apps with deep routes I prefer the resolver.
Why not return index.html from a custom 404 error page?
Because the response status would be 404 for every deep link, which breaks crawlers, monitoring and some proxies, and the same error handling would also catch API 404s. Forwarding gives a proper 200 for SPA routes and keeps real 404s for APIs and missing files.
Should the frontend use hash routing instead, like /#/orders/42?
Hash routing avoids the problem because the part after # is never sent to the server, but the URLs are uglier and some features, such as server-side analytics of paths, become harder. With the server-side fallback, normal history routing works and is the better default.
7Conclusion
In this answer, we saw that a deep link like /orders/42 returns 404 on refresh because the server has neither a file nor a controller for a route that exists only in the browser. We fixed it by forwarding paths without a dot to index.html, first with a controller using @GetMapping("/{path:[^\\.]*}") style patterns and then with a PathResourceResolver that falls back to index.html at any depth, while excluding /api/** and /actuator/** so API 404s stay JSON. We set no-cache on index.html and long immutable caching on hashed assets, and built the frontend with frontend-maven-plugin or a Docker build stage.
Open with
The 404 happens because the route only exists in the React router. Clicking a link changes the URL in the browser without a request, but a refresh sends GET /orders/42 to Spring Boot, which looks for a controller and then a static file and finds neither.
Then, if they want more
So I need the server to return index.html for paths that are SPA routes, and only for those.
For a flat app I add a controller with @GetMapping patterns like slash, path, regex without a dot, returning forward:/index.html; the dot rule means a missing JavaScript or image file still gets a real 404 instead of HTML, and I add a negative lookahead so the first segment can't be api or actuator.
For nested routes like /orders/42/items, I prefer a custom PathResourceResolver on the slash-star-star resource handler: if the file exists, serve it, if the path starts with api or actuator or contains a dot, return null so it's a 404, otherwise return index.html.
Resource handlers only run when no controller matched, so real APIs are untouched and unknown API paths stay JSON 404s. Then caching: hashed asset files get max-age of a year and immutable, and index.html gets no-cache so a deploy is picked up immediately.
And I build the frontend in the Maven build with frontend-maven-plugin or in a Docker build stage, rather than committing built files.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A React app built into static files is served from a Spring Boot jar under...": https://scrivio-interview-library.netlify.app/spring-boot/web-other/ca13e42e08/