The easiest way I've found is to implement a custom 404 page.
@Configuration public class MvcConfig { @Bean public EmbeddedServletContainerCustomizer notFoundCustomizer(){ return new NotFoundIndexTemplate(); } private static class NotFoundIndexTemplate implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/")); } } }
Neil McGuigan offers a HandlerInterceptor, but I could not figure out how this would be implemented. It would be great for me to see how this is implemented, since single-page applications using html5 history push state will want this behavior. And I actually did not find any recommendations on this issue.
source share