Skip to content

Instantly share code, notes, and snippets.

@srikarn
Last active October 28, 2023 08:55
Show Gist options
  • Save srikarn/e89cbb459c454754654b1975e8ca50c0 to your computer and use it in GitHub Desktop.
Save srikarn/e89cbb459c454754654b1975e8ca50c0 to your computer and use it in GitHub Desktop.
Spring boot configuration to forward to index page on 404
@Bean
ErrorViewResolver redirectToFrontEndOn404() {
return ( request, status, model ) -> status == HttpStatus.NOT_FOUND
? new ModelAndView("forward:/index.html", Collections.<String, Object>emptyMap(), HttpStatus.OK)
: null;
}
This is necessary on page refresh to avoid showing 404 or error pages
This forward also maintains the client route path, so angular will route to the correct route on reload.
This needs a complementary route config on the frontend, to redirect to default route on 404 on the client side
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment