When serving a single-page application (SPA) from a Spring Boot backend, client-side routes like /dashboard cause 404 errors on page refresh. Three approaches are covered to forward all unmatched routes to index.html: a catch-all controller using regex path patterns, a custom ErrorController that intercepts 404s, and a WebMvcConfigurer that registers a custom error page mapping. Each approach is demonstrated with code examples and tested via curl, with trade-offs noted (e.g., all three methods may inadvertently intercept backend API 404s).
Table of contents
1. Introduction2. Maven Dependency3. Setting Up a Basic SPA with Spring Boot4. Redirecting 404 Not Found Errors5. Test6. ConclusionSort: