SpringBoot中干掉Whitelabel Error Page,返回自定义内容

编程入门 行业动态 更新时间:2024-10-10 19:26:34

SpringBoot中干掉Whitelabel Error Page,返回<a href=https://www.elefans.com/category/jswz/34/1771438.html style=自定义内容"/>

SpringBoot中干掉Whitelabel Error Page,返回自定义内容

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: falsepath: /error

该配置类为ErrorProperties,默认的错误请求为/error,将 whitelabel 禁用后在 controller 中定义请求 /error返回自定义内容。

@Controller
public class ErrorController {@RequestMapping("/error")@ResponseBodypublic 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 容器中注入一个名为errorBean即可,返回类型为View:

@Configuration
public class MyConfig {@Bean("error")public View error() {return new MappingJackson2JsonView();}
}

结果当然是没问题的,会返回以下信息:

其中new MappingJackson2JsonView()返回的Json字符串的 View ,当然你也可以根据业务需求灵活使用,直接实现 View 接口。值得注意的是,这里的 request 并非我们直接请求的 request,使用request.getRequestURI()得到的结果会是/error,而非我们请求的那个不存在的路径。我们可以通过model集合进行获取,其key-value如上图所示,其中model.get("path")即可得到我们请求的那个不存在的路径。

@Configuration
public class MyConfig {@Bean("error")public View error() {return (model, request, response) -> {RWriterUtils.writeJson(response,RUtils.fail(RS.PAGE_NOT_FOUND, model.get("path")));};}
}

4. 总结

解决问题:

  1. 搜索引擎找解决方法
  2. 深入源码尝试自己解决

更多推荐

SpringBoot中干掉Whitelabel Error Page,返回自定义内容

本文发布于:2024-02-13 18:21:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   内容   Whitelabel   SpringBoot   Page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!