SpringBoot中干掉Whitelabel Error Page返回自定义内容的实现
1. 引言
SpringBoot中对于错误请求的页面是长这样的,

然而我们在访问在一些网站时,如果请求错误,一般都会有友好美观的提示,比如知乎这个,这比起一堆错误信息要友好的多了。

我们可以根据项目业务来自定义错误请求(RequestMapping中没有映射到的请求)的处理,比如返回自定义错误页面或者Json字符串。
2. 分析
我们看看SpringBoot中对于错误请求是如何处理的。SpringBoot项目中搜索Whitelabel定位到类WhitelabelErrorViewConfiguration,可以看到它是ErrorMvcAutoConfiguration的一个静态内部类,而且正是这个类处理的错误请求的,代码中的defaultErrorView正是我们看到的默认错误页面。
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)
@Conditional(ErrorMvcAutoConfiguration.ErrorTemplateMissingCondition.class)
protected static class WhitelabelErrorViewConfiguration {
private final ErrorMvcAutoConfiguration.StaticView defaultErrorView = new ErrorMvcAutoConfiguration.StaticView();
@Bean(name = "error")
@ConditionalOnMissingBean(name = "error")
public View defaultErrorView() {
return this.defaultErrorView;
}
// and so on...
}
仔细研究这个代码,我们不难发现,我们至少有两种方法可以替换掉默认的实现自定义错误页面。
入手点1:
@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)
入手点2:
@Bean(name = "error") @ConditionalOnMissingBean(name = "error")
3. 尝试
3.1 尝试 1
@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)
我们看到server.error.whitelabel.enabled控制了这个类是否装配,我们可以在配置中将其设为false
server: error: whitelabel: enabled: false path: /error
该配置类为ErrorProperties,默认的错误请求为/error,将 whitelabel 禁用后在 controller 中定义请求 /error返回自定义内容。
@Controller
public class ErrorController {
@RequestMapping("/error")
@ResponseBody
public R error(HttpServletRequest request) {
Integer status = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (Objects.nonNull(status)) {
HttpStatus httpStatus = HttpStatus.valueOf(status);
return RUtils.fail(httpStatus);
}
return RUtils.fail("Error");
}
}
运行!一顿操作猛如虎,仔细一看原地杵,想法不错,但疯狂报错:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/D:/Environment/Maven/repository/io/springfox/springfox-spring-web/2.9.2/springfox-spring-web-2.9.2.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/D:/Environment/Maven/repository/io/springfox/springfox-spring-web/2.9.2/springfox-spring-web-2.9.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [xxx/xxx/xxx/config/WebMvcConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'basicErrorController' method
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
to { /error}: There is already 'errorController' bean method
具体报错原因未作分析,怀疑是我项目设置有问题,笔者使用的SpringBoot版本是2.3.6.RELEASE,大家可以尝试一些行不行。既然第一个方法尝试接着尝试失败,那就试试第二个吧。
3.2 尝试2
@Bean(name = "error") @ConditionalOnMissingBean(name = "error")
这个操作更简单,我们只要向IoC 容器中注入一个名为error的Bean即可,返回类型为View:
@Configuration
public class MyConfig {
@Bean("error")
public View error() {
ModelAndView view = new ModelAndView(new MappingJackson2JsonView());
return view.getView();
}
}
其中new MappingJackson2JsonView()返回的Json字符串的 View ,当然你也可以根据业务需求灵活使用,如果想获取HttpServletRequest可以这样获取,但使用时需要注意判空,因为Bean实例化注入是可能获取不到HttpServletRequest而造成NPE,项目启动失败。
public static HttpServletRequest getRequest() {
return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
.map(r -> ((ServletRequestAttributes) r).getRequest())
.orElse(null);
}
结果当然是没问题的,会返回以下信息:

4. 总结
解决问题:
搜索引擎找解决方法深入源码尝试自己解决
到此这篇关于SpringBoot中干掉Whitelabel Error Page返回自定义内容的文章就介绍到这了,更多相关SpringBoot返回自定义内容内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
- SpringBoot启动异常Exception in thread “main“ java.lang.UnsupportedClassVersionError
- SpringBoot项目网页加载出现Whitelabel Error Page的解决
- SpringBoot项目报错:"Error starting ApplicationContext...."解决办法
- springboot ErrorPageFilter的实际应用详解
- SpringBoot自定义/error路径失效的解决
- SpringBoot异常: nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext解决方案
- Springboot初始化启动报错Error creating bean with name 'dataSource' defined in class path resource
相关文章
springboot使用@KafkaListener监听多个kafka配置实现
当服务中需要监听多个kafka时, 需要配置多个kafka,本文主要介绍了springboot使用@KafkaListener监听多个kafka配置实现,具有一定的参考价值,感兴趣的可以了解一下2024-04-04
Java class文件格式之属性详解_动力节点java学院整理
这篇文章主要介绍了Java class文件格式之属性详解,需要的朋友可以参考下2017-06-06
Spring容器-BeanFactory和ApplicationContext使用详解
这篇文章主要为大家介绍了Spring容器-BeanFactory和ApplicationContext的使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-04-04
Spring Boot 整合JPA 数据模型关联使用操作(一对一、一对多、多对多)
这篇文章主要介绍了Spring Boot 整合JPA 数据模型关联操作(一对一、一对多、多对多),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-07-07


最新评论