spring boot 不显示自定义错误页面

Ras*_*ari 6 java error-handling customization spring spring-boot

我向使用spring boot 2.3.1.RELEASE 的spring-boot-starter-thymeleaf项目添加了依赖项,并在其中放置了名称为src/main/resources/templates/error` 的文件,如下图所示:error.htmlsrc/main/resources/templateserror.html and other custom error pages inside

自定义错误页面文件

并在 application.yml 中添加此配置:

server:
  error:
    whitelabel:
      enabled: false
Run Code Online (Sandbox Code Playgroud)

ErrorMvcAutoConfiguration通过添加@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})Application类中排除。

但是,不幸的是,当错误发生时我看到下面的页面,例如 404 错误!

404错误页面

我怎么解决这个问题?我也用谷歌搜索了这个,但没有找到任何帮助。

dev*_*ess 4

尝试使用WebServerFactoryCustomizer

@Configuration
public class WebConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        factory.addErrorPages(
                new ErrorPage(HttpStatus.FORBIDDEN, "/403"),
                new ErrorPage(HttpStatus.NOT_FOUND, "/404"),
                new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500"));
    }
}
Run Code Online (Sandbox Code Playgroud)

和错误控制器:

@Controller
public class ErrorController {

    @GetMapping("/403")
    public String forbidden(Model model) {
        return "error/403";
    }

    @GetMapping("/404")
    public String notFound(Model model) {
        return "error/404";
    }

    @GetMapping("/500")
    public String internal(Model model) {
        return "error/500";
    }

    @GetMapping("/access-denied")
    public String accessDenied() {
        return "error/access-denied";
    }
}
Run Code Online (Sandbox Code Playgroud)

我有相同的结构并且它对我有用:

在此输入图像描述

示例:自定义错误消息

PS:在我的中,application.yml我没有任何错误处理属性