Spring Boot 和自定义 404 错误页面 [英] Spring Boot and custom 404 error page
本文介绍了Spring Boot 和自定义 404 错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 Spring Boot 应用程序中,我正在尝试配置自定义错误页面,例如对于 404,我在应用程序配置中添加了以下 Bean:
@Bean公共 EmbeddedServletContainerCustomizer containerCustomizer() {返回新的 EmbeddedServletContainerCustomizer() {@覆盖公共无效定制(ConfigurableEmbeddedServletContainer容器){container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));}};}
此外,我创建了以下简单的 Thymeleaf 模板:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><头><title>404 Not Found</title><meta charset="utf-8"/>头部><身体><h3>404 未找到</h3><h1 th:text="${errorCode}">404</h1><p th:utext="${errorMessage}">错误 java.lang.NullPointerException</p><a href="/" th:href="@{/}">返回首页</a>