詳解Springboot自定義異常處理
背景
Springboot 默認(rèn)把異常的處理集中到一個(gè)ModelAndView中了,但項(xiàng)目的實(shí)際過(guò)程中,這樣做,并不能滿足我們的要求。具體的自定義異常的處理,參看以下
具體實(shí)現(xiàn)
如果仔細(xì)看完spring boot的異常處理詳解,并且研究過(guò)源碼后,我覺(jué)得具體的實(shí)現(xiàn)可以不用看了。。。
重寫(xiě)定義錯(cuò)誤頁(yè)面的url,默認(rèn)只有一個(gè)/error
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer(){
return new EmbeddedServletContainerCustomizer(){
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
container.addErrorPages(new ErrorPage(java.lang.Throwable.class,"/error/500"));
}
};
}
重寫(xiě)通過(guò)實(shí)現(xiàn)ErrorController,重寫(xiě)B(tài)asicErrorController的功能實(shí)現(xiàn)
/**
* 重寫(xiě)B(tài)asicErrorController,主要負(fù)責(zé)系統(tǒng)的異常頁(yè)面的處理以及錯(cuò)誤信息的顯示
* @see org.springframework.boot.autoconfigure.web.BasicErrorController
* @see org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
*
* @author Jonathan
* @version 2016/5/31 11:22
* @since JDK 7.0+
*/
@Controller
@RequestMapping(value = "error")
@EnableConfigurationProperties({ServerProperties.class})
public class ExceptionController implements ErrorController {
private ErrorAttributes errorAttributes;
@Autowired
private ServerProperties serverProperties;
/**
* 初始化ExceptionController
* @param errorAttributes
*/
@Autowired
public ExceptionController(ErrorAttributes errorAttributes) {
Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
this.errorAttributes = errorAttributes;
}
/**
* 定義404的ModelAndView
* @param request
* @param response
* @return
*/
@RequestMapping(produces = "text/html",value = "404")
public ModelAndView errorHtml404(HttpServletRequest request,
HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
return new ModelAndView("error/404", model);
}
/**
* 定義404的JSON數(shù)據(jù)
* @param request
* @return
*/
@RequestMapping(value = "404")
@ResponseBody
public ResponseEntity<Map<String, Object>> error404(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* 定義500的ModelAndView
* @param request
* @param response
* @return
*/
@RequestMapping(produces = "text/html",value = "500")
public ModelAndView errorHtml500(HttpServletRequest request,
HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
return new ModelAndView("error/500", model);
}
/**
* 定義500的錯(cuò)誤JSON信息
* @param request
* @return
*/
@RequestMapping(value = "500")
@ResponseBody
public ResponseEntity<Map<String, Object>> error500(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* Determine if the stacktrace attribute should be included.
* @param request the source request
* @param produces the media type produced (or {@code MediaType.ALL})
* @return if the stacktrace attribute should be included
*/
protected boolean isIncludeStackTrace(HttpServletRequest request,
MediaType produces) {
ErrorProperties.IncludeStacktrace include = this.serverProperties.getError().getIncludeStacktrace();
if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
return true;
}
if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
return getTraceParameter(request);
}
return false;
}
/**
* 獲取錯(cuò)誤的信息
* @param request
* @param includeStackTrace
* @return
*/
private Map<String, Object> getErrorAttributes(HttpServletRequest request,
boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(requestAttributes,
includeStackTrace);
}
/**
* 是否包含trace
* @param request
* @return
*/
private boolean getTraceParameter(HttpServletRequest request) {
String parameter = request.getParameter("trace");
if (parameter == null) {
return false;
}
return !"false".equals(parameter.toLowerCase());
}
/**
* 獲取錯(cuò)誤編碼
* @param request
* @return
*/
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
try {
return HttpStatus.valueOf(statusCode);
}
catch (Exception ex) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
/**
* 實(shí)現(xiàn)錯(cuò)誤路徑,暫時(shí)無(wú)用
* @see ExceptionMvcAutoConfiguration#containerCustomizer()
* @return
*/
@Override
public String getErrorPath() {
return "";
}
}
總結(jié)
第一步,通過(guò)定義containerCustomizer,重寫(xiě)定義了異常處理對(duì)應(yīng)的視圖。目前定義了404和500,可以繼續(xù)擴(kuò)展。
第二步,重寫(xiě)B(tài)asicErrorController,當(dāng)然可以直接定義一個(gè)普通的controller類(lèi),直接實(shí)現(xiàn)第一步定義的視圖的方法。重寫(xiě)的目的是重用ErrorAttributes。這樣在頁(yè)面,直接可以獲取到status,message,exception,trace等內(nèi)容。
如果僅僅是把異常處理的視圖作為靜態(tài)頁(yè)面,不需要看到異常信息內(nèi)容的話,直接第一步后,再定義error/404,error/500等靜態(tài)視圖即可。
ErrorController根據(jù)Accept頭的內(nèi)容,輸出不同格式的錯(cuò)誤響應(yīng)。比如針對(duì)瀏覽器的請(qǐng)求生成html頁(yè)面,針對(duì)其它請(qǐng)求生成json格式的返回
以上兩步的操作,比網(wǎng)上流傳的更能實(shí)現(xiàn)自定義化。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
記一次集成swagger2(Knife4j)在線文檔提示:Knude4j文檔請(qǐng)求異常的解決辦法
Knife4j是一個(gè)集Swagger2 和 OpenAPI3為一體的增強(qiáng)解決方案,下面這篇文章主要給大家介紹了關(guān)于一次集成swagger2(Knife4j)在線文檔提示:Knude4j文檔請(qǐng)求異常的解決辦法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
Java?Mybatis的初始化之Mapper.xml映射文件的詳解
這篇文章主要介紹了Java?Mybatis的初始化之Mapper.xml映射文件的詳解,解析完全局配置文件后接下來(lái)就是解析Mapper文件了,它是通過(guò)XMLMapperBuilder來(lái)進(jìn)行解析的2022-08-08
解決springcloud阿里云OSS文件訪問(wèn)跨域問(wèn)題的實(shí)現(xiàn)
本文主要介紹了解決springcloud阿里云OSS文件訪問(wèn)跨域問(wèn)題的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
SpringBoot中定時(shí)任務(wù)@Scheduled注解的使用解讀
這篇文章主要介紹了SpringBoot中定時(shí)任務(wù)@Scheduled注解的使用解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
SpringCloud Feign Jackson自定義配置方式
這篇文章主要介紹了SpringCloud Feign Jackson自定義配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBatch數(shù)據(jù)寫(xiě)入實(shí)現(xiàn)
Spring Batch通過(guò)ItemWriter接口及其豐富的實(shí)現(xiàn),提供了強(qiáng)大的數(shù)據(jù)寫(xiě)入能力,本文主要介紹了SpringBatch數(shù)據(jù)寫(xiě)入實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04
SpringBoot 動(dòng)態(tài)定時(shí)器的使用方法
這篇文章主要介紹了SpringBoot 動(dòng)態(tài)定時(shí)器的使用方法,非常不錯(cuò),具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下2018-05-05

