简体   繁体   中英

Why doesn't my spring boot error controller work?

I have a simple spring boot web application (running under Tomcat) and I cannot figure out how to get an error handler to work. So far I've created the web application and controllers and they all work fine. That is, they display Thymeleaf templates. Upon error. I get the default white label error page.

I first disabled this white label error page by adding an EnableAutoConfiguration annotation, thus:

@SpringBootApplication(scanBasePackages="au.com.sample")
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
public class SampleWebApp extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SampleWebApp.class);
    }
}

Now, if I browse to an unknown page I get a generic 404 HTML error page containing:

<html>...<body><h1>HTTP Status 404 – Not Found</h1></body></html>

So that step worked. Now when I try and add my own error controller it does not trigger when I browse to an end point that does not exist:

@Controller
@RequestMapping("/")
public class ErrorHandlingController implements ErrorController {
    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping(/"error")
    public ModelAndView handleError() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("title", "Error");
        mav.setViewName("layout");
        return mav;
    }
}

"layout" is my existing thymeleaf template that works fine for my other end points.

I've tried several variations of the error controller, eg. changing the endpoint to "error" (not "/error" ), using @RestController instead of @Controller but with no luck. In every case, I get the same generic 404 HTML instead of my hand-crafted template.

Can anyone see what I'm doing wrong or have any tips on how to track down my problem.

To show a custom error page for a specific status is pretty easy in Spring Boot. Just add an <error-code>.html into the src/main/resources/templates/errors directory to have it resolved. Or add a generic error.html as a fallback. See the reference guide for this feature (see also this ).

If you want to add your own error handling just add a bean of type ErrorController or if you want to only add attributes add an ErrorAttributes typed bean to your configuration. See this section for more information.

A late answer to the original question of why Spring doesn't call your error controller: I think you are excluding the magic that makes the custom ErrorController work. In a Spring-Boot v2.1.6 app I had to allow (NOT exclude) the ErrorMvcAutoConfiguration class, then it used a custom MyController-implements-ErrorController class on page-not-found situations etc. It seems to be an either-or situation. I think you should only exclude the ErrorMvc.. class if you are using Thymeleaf templates AND you have published template files in a templates/ directory, because in that case you don't really need a custom error controller. HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM