SpringBoot?錯(cuò)誤頁(yè)面跳轉(zhuǎn)方式
SpringBoot錯(cuò)誤頁(yè)面跳轉(zhuǎn)
SpringBoot實(shí)現(xiàn)MVC 404、500等錯(cuò)誤時(shí)跳轉(zhuǎn)自定義頁(yè)面
一、新增配置類
package com.study.demo.config;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
/**
?* 錯(cuò)誤頁(yè)面的配置
?*/
@Component
public class ErrorPageConfig implements ErrorPageRegistrar {
? ? @Override
? ? public void registerErrorPages(ErrorPageRegistry registry) {
? ? ? ? ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/errorPageController/error_400");
? ? ? ? ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/errorPageController/error_401");
? ? ? ? ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/errorPageController/error_404");
? ? ? ? ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/errorPageController/error_500");
? ? ? ? registry.addErrorPages(error400Page,error401Page,error404Page,error500Page);
? ? }
}二、錯(cuò)誤頁(yè)面跳轉(zhuǎn)控制器
package com.study.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/errorPageController")
public class ErrorPageController {
? ? @RequestMapping("/error_{errorCode}")
? ? public String error(@PathVariable int errorCode){
? ? ? ? String responseMsg;
? ? ? ? switch (errorCode) {
? ? ? ? ? ? case 400: responseMsg = "/400.html"; break;
? ? ? ? ? ? case 401: responseMsg = "/401.html"; break;
? ? ? ? ? ? case 404: responseMsg = "/404.html"; break;
? ? ? ? ? ? case 500: responseMsg = "/500.html"; break;
? ? ? ? ? ? default: responseMsg = "/404.html"; break;
? ? ? ? }
? ? ? ? return responseMsg;
? ? }
}SpringBoot自定義錯(cuò)誤頁(yè)面
一、錯(cuò)誤頁(yè)面
請(qǐng)求出現(xiàn)錯(cuò)誤時(shí),跳轉(zhuǎn)到自定義的頁(yè)面中,比如404,假如沒對(duì)錯(cuò)誤進(jìn)行處理,那么系統(tǒng)默認(rèn)的頁(yè)面與項(xiàng)目的頁(yè)面會(huì)有很大的不搭。
解決:在默認(rèn)的靜態(tài)路徑下,新建error文件,里面放入錯(cuò)誤頁(yè)面,頁(yè)面命名為錯(cuò)誤狀態(tài)碼,如:404.html,也可以命名為4xx.html,但如果兩個(gè)文件同時(shí)存在,那么會(huì)優(yōu)先展示404.html
注:靜態(tài)路徑為
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/",
?"classpath:/resources/",?
?"classpath:/static/",?
?"classpath:/public/"
?};
?// 注:還有一個(gè)默認(rèn)的根路徑 ? ?"/"二、處理過程
出現(xiàn)4xx或5xx錯(cuò)誤時(shí),ErrorPageCustomizer生效,就會(huì)來到/error請(qǐng)求,就會(huì)被BasicErrorController處理。
//在DefaultErrorViewResolver中有一段代碼
// 處理4xx和5xx的請(qǐng)求
static {
Map<Series, String> views = new EnumMap(Series.class);
views.put(Series.CLIENT_ERROR, "4xx");
views.put(Series.SERVER_ERROR, "5xx");
SERIES_VIEWS = Collections.unmodifiableMap(views);
}
// 解析,并會(huì)跳轉(zhuǎn)到error/錯(cuò)誤狀態(tài)碼; 頁(yè)面中
private ModelAndView resolve(String viewName, Map<String, Object> model) {
String errorViewName = "error/" + viewName;
TemplateAvailabilityProvider provider = this.templateAvailabilityProviders.getProvider(errorViewName, this.applicationContext);
// 對(duì)是否有模板引擎做出相應(yīng)的視圖處理
return provider != null ? new ModelAndView(errorViewName, model) : this.resolveResource(errorViewName, model);
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)String類型和Date類型相互轉(zhuǎn)換
很多人表示,java將string類型轉(zhuǎn)為date類型不知道應(yīng)該怎樣做,本文就來介紹一下java實(shí)現(xiàn)String類型和Date類型相互轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
java中Hutool工具類的常見使用場(chǎng)景詳解
在日常開發(fā)中,我們會(huì)使用很多工具類來提升項(xiàng)目開發(fā)的速度,而國(guó)內(nèi)用的比較多的 Hutool 框架,就是其中之一,本文我們就來介紹一下Hutool的具體使用吧2023-12-12
ElasticSearch學(xué)習(xí)之Es集群Api操作示例
這篇文章主要為大家介紹了ElasticSearch學(xué)習(xí)之Es集群Api操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
maven <repositories>標(biāo)簽和<pluginRepositories>標(biāo)簽的使用
這篇文章主要介紹了maven <repositories>標(biāo)簽和<pluginRepositories>標(biāo)簽的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
java?fastjson傳輸long數(shù)據(jù)卻接收到了int的問題
這篇文章主要介紹了java?fastjson傳輸long數(shù)據(jù)卻接收到了int的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
java+mysql模擬實(shí)現(xiàn)銀行系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java+mysql模擬實(shí)現(xiàn)銀行系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05

