欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot中干掉Whitelabel Error Page返回自定義內(nèi)容的實(shí)現(xiàn)

 更新時(shí)間:2021年01月05日 11:35:05   作者:MylesYang  
這篇文章主要介紹了SpringBoot中干掉Whitelabel Error Page返回自定義內(nèi)容的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

 1. 引言

SpringBoot中對(duì)于錯(cuò)誤請(qǐng)求的頁(yè)面是長(zhǎng)這樣的,

Whitelabel Error Page

然而我們?cè)谠L問(wèn)在一些網(wǎng)站時(shí),如果請(qǐng)求錯(cuò)誤,一般都會(huì)有友好美觀的提示,比如知乎這個(gè),這比起一堆錯(cuò)誤信息要友好的多了。

BHu

我們可以根據(jù)項(xiàng)目業(yè)務(wù)來(lái)自定義錯(cuò)誤請(qǐng)求(RequestMapping中沒(méi)有映射到的請(qǐng)求)的處理,比如返回自定義錯(cuò)誤頁(yè)面或者Json字符串。

2. 分析

我們看看SpringBoot中對(duì)于錯(cuò)誤請(qǐng)求是如何處理的。SpringBoot項(xiàng)目中搜索Whitelabel定位到類WhitelabelErrorViewConfiguration,可以看到它是ErrorMvcAutoConfiguration的一個(gè)靜態(tài)內(nèi)部類,而且正是這個(gè)類處理的錯(cuò)誤請(qǐng)求的,代碼中的defaultErrorView正是我們看到的默認(rèn)錯(cuò)誤頁(yè)面。

@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...
}

仔細(xì)研究這個(gè)代碼,我們不難發(fā)現(xiàn),我們至少有兩種方法可以替換掉默認(rèn)的實(shí)現(xiàn)自定義錯(cuò)誤頁(yè)面。

入手點(diǎn)1:

@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)

入手點(diǎn)2:

@Bean(name = "error")
@ConditionalOnMissingBean(name = "error")

3. 嘗試

3.1 嘗試 1

@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)

我們看到server.error.whitelabel.enabled控制了這個(gè)類是否裝配,我們可以在配置中將其設(shè)為false

server:
 error:
  whitelabel:
   enabled: false
  path: /error

該配置類為ErrorProperties,默認(rèn)的錯(cuò)誤請(qǐng)求為/error,將 whitelabel 禁用后在 controller 中定義請(qǐng)求 /error返回自定義內(nèi)容。

@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");
	}

}

運(yùn)行!一頓操作猛如虎,仔細(xì)一看原地杵,想法不錯(cuò),但瘋狂報(bào)錯(cuò):

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

具體報(bào)錯(cuò)原因未作分析,懷疑是我項(xiàng)目設(shè)置有問(wèn)題,筆者使用的SpringBoot版本是2.3.6.RELEASE,大家可以嘗試一些行不行。既然第一個(gè)方法嘗試接著嘗試失敗,那就試試第二個(gè)吧。

3.2 嘗試2

@Bean(name = "error")
@ConditionalOnMissingBean(name = "error")

這個(gè)操作更簡(jiǎn)單,我們只要向IoC 容器中注入一個(gè)名為errorBean即可,返回類型為View:

@Configuration
public class MyConfig {
  @Bean("error")
	public View error() {
		ModelAndView view = new ModelAndView(new MappingJackson2JsonView());
		return view.getView();
	}
}

其中new MappingJackson2JsonView()返回的Json字符串的 View ,當(dāng)然你也可以根據(jù)業(yè)務(wù)需求靈活使用,如果想獲取HttpServletRequest可以這樣獲取,但使用時(shí)需要注意判空,因?yàn)锽ean實(shí)例化注入是可能獲取不到HttpServletRequest而造成NPE,項(xiàng)目啟動(dòng)失敗。

public static HttpServletRequest getRequest() {
  return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
     .map(r -> ((ServletRequestAttributes) r).getRequest())
     .orElse(null);
}

結(jié)果當(dāng)然是沒(méi)問(wèn)題的,會(huì)返回以下信息:

result

4. 總結(jié)

解決問(wèn)題:

搜索引擎找解決方法深入源碼嘗試自己解決

到此這篇關(guān)于SpringBoot中干掉Whitelabel Error Page返回自定義內(nèi)容的文章就介紹到這了,更多相關(guān)SpringBoot返回自定義內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringMVC中的@RequestMapping注解解析

    SpringMVC中的@RequestMapping注解解析

    這篇文章主要介紹了SpringMVC中的@RequestMapping注解解析,SpringMVC使用@RequestMapping注解為控制器指定可以處理哪些?URL?請(qǐng)求,在控制器的類定義及方法定義處都可標(biāo)注@RequestMapping,需要的朋友可以參考下
    2023-12-12
  • 詳解mybatis.generator配上最新的mysql 8.0.11的一些坑

    詳解mybatis.generator配上最新的mysql 8.0.11的一些坑

    這篇文章主要介紹了詳解mybatis.generator配上最新的mysql 8.0.11的一些坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • 關(guān)于@Value注入List,Map及設(shè)置默認(rèn)值問(wèn)題

    關(guān)于@Value注入List,Map及設(shè)置默認(rèn)值問(wèn)題

    這篇文章主要介紹了@Value注入List,Map及設(shè)置默認(rèn)值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 詳解Log4j 日志文件存放位置設(shè)置

    詳解Log4j 日志文件存放位置設(shè)置

    這篇文章主要介紹了詳解Log4j 日志文件存放位置設(shè)置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Java簡(jiǎn)易抽獎(jiǎng)系統(tǒng)小項(xiàng)目

    Java簡(jiǎn)易抽獎(jiǎng)系統(tǒng)小項(xiàng)目

    這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)易抽獎(jiǎng)系統(tǒng)小項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 通過(guò)Java實(shí)現(xiàn)在Word中創(chuàng)建可填充表單

    通過(guò)Java實(shí)現(xiàn)在Word中創(chuàng)建可填充表單

    這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Java代碼,以編程方式在Word中創(chuàng)建可填充表單,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-03-03
  • 詳解Spring事務(wù)回滾的兩種方法

    詳解Spring事務(wù)回滾的兩種方法

    Spring事務(wù)回滾的前提是你當(dāng)前使用的數(shù)據(jù)庫(kù)必須支持事務(wù),比如MySQL的Innodb是支持的,但Mysaim則是不支持事務(wù)的,本文就給大家介紹兩種Spring事務(wù)回滾的方法,需要的朋友可以參考下
    2023-07-07
  • java哈夫曼樹(shù)實(shí)例代碼

    java哈夫曼樹(shù)實(shí)例代碼

    這篇文章主要為大家介紹了java哈夫曼樹(shù)實(shí)例代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 深入學(xué)習(xí)Java中的SPI機(jī)制

    深入學(xué)習(xí)Java中的SPI機(jī)制

    這篇文章主要介紹了深入學(xué)習(xí)Java中的SPI機(jī)制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Spring框架通過(guò)工廠創(chuàng)建Bean的三種方式實(shí)現(xiàn)

    Spring框架通過(guò)工廠創(chuàng)建Bean的三種方式實(shí)現(xiàn)

    這篇文章主要介紹了Spring框架通過(guò)工廠創(chuàng)建Bean的三種方式實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03

最新評(píng)論