網(wǎng)站后臺(tái)統(tǒng)計(jì)代碼福州網(wǎng)站seo公司
定義錯(cuò)誤頁面
SpringBoot 默認(rèn)的處理異常的機(jī)制:SpringBoot 默認(rèn)的已經(jīng)提供了一套處理異常的機(jī)制。一旦程序中出現(xiàn)了異常 SpringBoot 會(huì)像/error 的 url 發(fā)送請求。在 springBoot 中提供了一個(gè)叫 BasicExceptionController?來處理/error 請求,然后跳轉(zhuǎn)到默認(rèn)顯示異常的頁面來展示異常信息
如 果 我 們 需 要 將 所 有 的 異 常 同 一 跳 轉(zhuǎn) 到 自 定 義 的 錯(cuò) 誤 頁 面 , 需 要 在src/main/resources/templates 目錄下創(chuàng)建 error.html 頁面。注意:名稱必須叫 error
?
?@ExceptionHandler 處理
針對特定的異常做出不同的處理,我們可以通過@ExceptionHandle來處理實(shí)現(xiàn),具體如下
@Controller
public class UserController {/*** 模擬 NullPointerException* @return*/@RequestMapping("/show1")public String showInfo(){String str = null;str.length();return "index";}/*** 模擬 ArithmeticException* @return*/@RequestMapping("/show2")public String showInfo2(){int a = 10/0;return "index";}/*** java.lang.ArithmeticException* 該方法需要返回一個(gè) ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定* 參數(shù) Exception e:會(huì)將產(chǎn)生異常對象注入到方法中*/@ExceptionHandler(value={java.lang.ArithmeticException.class})public ModelAndView arithmeticExceptionHandler(Exception e){ModelAndView mv = new ModelAndView();mv.addObject("error", e.toString());mv.setViewName("error1");return mv;}/*** java.lang.NullPointerException* 該方法需要返回一個(gè) ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定* 參數(shù) Exception e:會(huì)將產(chǎn)生異常對象注入到方法中*/@ExceptionHandler(value={java.lang.NullPointerException.class})public ModelAndView nullPointerExceptionHandler(Exception e){ModelAndView mv = new ModelAndView();mv.addObject("error", e.toString());mv.setViewName("error2");return mv;}
}
?錯(cuò)誤頁面
<!DOCTYPE html>
<html lang="en" >
<head><meta charset="UTF-8"><title>錯(cuò)誤頁面</title>
</head>
<body>
出錯(cuò)了,請與管理員聯(lián)系。。。錯(cuò)誤提示頁面-ArithmeticException<br>
<span th:text="${error}"></span>
</body>
</html>
<!DOCTYPE html>
<html lang="en" >
<head><meta charset="UTF-8"><title>錯(cuò)誤頁面</title>
</head>
<body>
出錯(cuò)了,請與管理員聯(lián)系。。。錯(cuò)誤提示頁面-NullPointerException <br>
<span th:text="${error}"></span>
</body>
</html>
?
?@ControllerAdvice+@ExceptionHandler處理
第二種處理方式中,異常處理的代碼和業(yè)務(wù)代碼放在一個(gè)類中了,這種方式耦合性太強(qiáng)了,最好是將業(yè)務(wù)和異常處理的代碼分離開,這時(shí)我們可以定義一個(gè)專門的異常處理類,通過注解@ControllerAdvice來實(shí)現(xiàn)。具體如下:
@ControllerAdvice
public class GlobalException {/*** java.lang.ArithmeticException* 該方法需要返回一個(gè) ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定* 參數(shù) Exception e:會(huì)將產(chǎn)生異常對象注入到方法中*/@ExceptionHandler(value={java.lang.ArithmeticException.class})public ModelAndView arithmeticExceptionHandler(Exception e){ModelAndView mv = new ModelAndView();mv.addObject("error", e.toString()+" -- advice");mv.setViewName("error1");return mv;}/*** java.lang.NullPointerException* 該方法需要返回一個(gè) ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定* 參數(shù) Exception e:會(huì)將產(chǎn)生異常對象注入到方法中*/@ExceptionHandler(value={java.lang.NullPointerException.class})public ModelAndView nullPointerExceptionHandler(Exception e){ModelAndView mv = new ModelAndView();mv.addObject("error", e.toString()+" -- advice");mv.setViewName("error2");return mv;}
}
控制器中實(shí)現(xiàn)
@Controller
public class UserController {/*** 模擬 NullPointerException* @return*/@RequestMapping("/show1")public String showInfo(){String str = null;str.length();return "index";}/*** 模擬 ArithmeticException* @return*/@RequestMapping("/show2")public String showInfo2(){int a = 10/0;return "index";}
}
SimpleMappingExceptionResolver處理?
我們還可以通過SimpleMappingExceptionResolver將具體的異常和錯(cuò)誤頁面指定對應(yīng)關(guān)系,這樣就不用每個(gè)異常都單獨(dú)寫一個(gè)方法了。
@Configuration
public class GlobalException {/*** 該方法必須要有返回值。返回值類型必須是:SimpleMappingExceptionResolver*/@Beanpublic SimpleMappingExceptionResolvergetSimpleMappingExceptionResolver(){SimpleMappingExceptionResolver resolver = newSimpleMappingExceptionResolver();Properties mappings = new Properties();/*** 參數(shù)一:異常的類型,注意必須是異常類型的全名* 參數(shù)二:視圖名稱*/mappings.put("java.lang.ArithmeticException", "error1");mappings.put("java.lang.NullPointerException","error2");//設(shè)置異常與視圖映射信息的resolver.setExceptionMappings(mappings);return resolver;}
}
自定義HandlerExceptionResolver處理
最后我們還可以通過實(shí)現(xiàn)HandlerExceptionResolver 接口來根據(jù)不同異常類型來動(dòng)態(tài)處理異常。
@Configuration
public class GlobalException implements HandlerExceptionResolver {/*** @param httpServletRequest* @param httpServletResponse* @param o* @param e* @return*/@Overridepublic ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) {ModelAndView mv = new ModelAndView();//判斷不同異常類型,做不同視圖跳轉(zhuǎn)if (ex instanceof ArithmeticException) {mv.setViewName("error1");}if (ex instanceof NullPointerException) {mv.setViewName("error2");}mv.addObject("error", ex.toString());return mv;}
}