spring boot doesn#39;t shows custom error pages(Spring Boot不显示自定义错误页)
问题描述
我将spring-boot-starter-thymeleaf
依赖项添加到使用Spring Boot 2.3.1.RELEASE的项目中,并将error.html
放在src/main/resources/templates
文件中,名为error.html and other custom error pages inside
src/main/resource/plates/error`,如下图所示:
并在Application.yml:
中添加此配置server:
error:
whitelabel:
enabled: false
并通过将@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})
添加到Application
类中来排除ErrorMvcAutoConfiguration
。
但是,不幸的是,当错误发生时,我在下面的页面上看到了这个,例如404错误!
如何解决此问题?我也在谷歌上搜索了一下,但没有找到任何可以帮助的东西。
推荐答案
尝试使用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"));
}
}
和错误控制器:
@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";
}
}
我有相同的结构,它对我有效:
示例:Customize the Error Messages
ps:在我的application.yml
中,我没有任何用于错误处理的属性
这篇关于Spring Boot不显示自定义错误页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Spring Boot不显示自定义错误页


- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01