Whitelabel Error Page

 
错误:<meta charset="UTF-8" >

只是因为少了一个【/】,结束标签,耽误了有一个小时至少。。哎!!
正确:<meta charset="UTF-8" />


 

 
 
### Spring Boot Whitelabel Error Page 解决方案 Spring Boot 的 Whitelabel Error Page 是默认的错误页面,当应用程序中发生未处理的异常时会显示该页面。以下是一些解决方案和原因分析: #### 1. **自定义全局错误处理** 可以通过实现 `ErrorController` 接口来自定义错误页面。创建一个类并实现 `ErrorController` 接口,重写 `getErrorPath` 和 `error` 方法[^1]。 例如: ```java import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServletRequest; import java.util.Map; @RestController public class CustomErrorController implements ErrorController { @RequestMapping("/error") public Map<String, Object> handleError(HttpServletRequest request) { // 获取错误信息 ErrorAttributeOptions options = ErrorAttributeOptions.defaults(); Map<String, Object> errorAttributes = getErrorAttributes(request, options); return errorAttributes; } @Override public String getErrorPath() { return "/error"; } } ``` #### 2. **使用静态资源文件作为错误页面** 可以在 `src/main/resources/static` 目录下创建 HTML 文件来定义自定义错误页面。例如,创建一个名为 `error.html` 的文件[^3]。 示例内容如下: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Error Page</title> </head> <body> <h1>An error occurred!</h1> <p>Please try again later.</p> </body> </html> ``` #### 3. **配置 `application.properties` 或 `application.yml`** 可以通过设置 `server.error.whitelabel.enabled=false` 来禁用 Whitelabel Error Page,并启用自定义错误页面[^1]。 例如,在 `application.properties` 中添加: ```properties server.error.whitelabel.enabled=false ``` #### 4. **扩展 `DefaultErrorAttributes` 类** 如果需要自定义错误响应中的属性,可以扩展 `DefaultErrorAttributes` 类并注册为 Bean[^2]。 示例代码如下: ```java import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.stereotype.Component; import org.springframework.web.context.request.WebRequest; import java.util.Map; @Component public class CustomErrorAttributes extends DefaultErrorAttributes { @Override public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options); errorAttributes.put("customMessage", "This is a custom error message."); return errorAttributes; } } ``` #### 5. **使用 `ErrorPageRegistrar` 接口** 对于不使用 Spring MVC 的应用程序,可以使用 `ErrorPageRegistrar` 接口直接注册错误页面。 示例代码如下: ```java import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.ErrorPageRegistrar; import org.springframework.boot.web.server.ErrorPageRegistry; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; @Configuration public class ErrorPageConfig { @Bean public ErrorPageRegistrar errorPageRegistrar() { return new ErrorPageRegistrar() { @Override public void registerErrorPages(ErrorPageRegistry registry) { ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"); ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html"); registry.addErrorPages(errorPage404, errorPage500); } }; } } ``` #### 6. **MyBatis 和 MyBatis-Plus 配置冲突** 如果项目中同时使用了 MyBatis 和 MyBatis-Plus,可能会导致某些异常未被捕获,从而触发 Whitelabel Error Page。确保 Mapper 文件路径正确配置,并在 `application.properties` 中指定 Mapper XML 文件的位置[^4]。 例如: ```properties mybatis.mapper-locations=classpath*:mapper/*.xml ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值