Controller based error pages spring thymeleaf

Viewed 8204

I've managed to build static error pages and redirect to them with this bean:

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {

        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                if (optiuniEnvironment.equals("development")) {
                    ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
                    ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
                    ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/404.html");

                    container.addErrorPages(error401Page, error404Page, error500Page);
                }
            }
        };
    }

However, now I want to build a custom page, that uses the functionality of the controller.

So in my controller I have something like

    @RequestMapping("/404.html")
    String pageNotFound(Model model, HttpServletRequest request)
    {
        return "404";
    }

and wish to redirect to it if I encounter the HttpStatus 404.

I believe the answer is configuring the DispatcherServlet, but not sure. Any ideas? Thanks!

(if possible, please use java-based configurations, not xml)

2 Answers
Related