简体   繁体   English

为什么我的弹簧启动错误控制器不起作用?

[英]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. 我有一个简单的Spring启动Web应用程序(在Tomcat下运行),我无法弄清楚如何使错误处理程序工作。 So far I've created the web application and controllers and they all work fine. 到目前为止,我已经创建了Web应用程序和控制器,它们都运行良好。 That is, they display Thymeleaf templates. 也就是说,它们显示Thymeleaf模板。 Upon error. 出错了。 I get the default white label error page. 我得到默认的白标错误页面。

I first disabled this white label error page by adding an EnableAutoConfiguration annotation, thus: 我首先通过添加EnableAutoConfiguration注释来禁用此白标错误页面,因此:

@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: 现在,如果我浏览到一个未知页面,我会得到一个通用的404 HTML错误页面,其中包含:

<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. "layout"是我现有的百日咳模板,适用于我的其他终点。

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. 使用@RestController而不是@Controller将端点更改为"error" (不是"/error" ),但没有运气。 In every case, I get the same generic 404 HTML instead of my hand-crafted template. 在每种情况下,我都使用相同的通用404 HTML而不是我手工制作的模板。

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. 在Spring Boot中显示特定状态的自定义错误页面非常简单。 Just add an <error-code>.html into the src/main/resources/templates/errors directory to have it resolved. 只需将<error-code>.htmlsrc/main/resources/templates/errors目录即可解析。 Or add a generic error.html as a fallback. 或者添加通用error.html作为后备。 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. 如果要添加自己的错误处理,只需添加ErrorController类型的bean,或者如果只想添加属性,请在配置中添加ErrorAttributes类型的bean。 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. 对于为什么Spring不会调用错误控制器的原始问题的最新答案:我认为你排除了使自定义ErrorController工作的魔力。 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. 在Spring-Boot v2.1.6应用程序中,我必须允许(不排除)ErrorMvcAutoConfiguration类,然后它在页面未找到的情况等上使用自定义的MyController-implements-ErrorController类。它似乎是一个或者两种情况。 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. 我认为你应该只排除ErrorMvc ..类,如果你使用Thymeleaf模板并且你已经在模板/目录中发布了模板文件,因为在这种情况下你真的不需要自定义错误控制器。 HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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