網(wǎng)站后臺統(tǒng)計代碼網(wǎng)站怎么seo關(guān)鍵詞排名優(yōu)化推廣
定義錯誤頁面
SpringBoot 默認(rèn)的處理異常的機制:SpringBoot 默認(rèn)的已經(jīng)提供了一套處理異常的機制。一旦程序中出現(xiàn)了異常 SpringBoot 會像/error 的 url 發(fā)送請求。在 springBoot 中提供了一個叫 BasicExceptionController?來處理/error 請求,然后跳轉(zhuǎn)到默認(rèn)顯示異常的頁面來展示異常信息
如 果 我 們 需 要 將 所 有 的 異 常 同 一 跳 轉(zhuǎn) 到 自 定 義 的 錯 誤 頁 面 , 需 要 在src/main/resources/templates 目錄下創(chuàng)建 error.html 頁面。注意:名稱必須叫 error
?
?@ExceptionHandler 處理
針對特定的異常做出不同的處理,我們可以通過@ExceptionHandle來處理實現(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* 該方法需要返回一個 ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定* 參數(shù) Exception e:會將產(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* 該方法需要返回一個 ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定* 參數(shù) Exception e:會將產(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;}
}
?錯誤頁面
<!DOCTYPE html>
<html lang="en" >
<head><meta charset="UTF-8"><title>錯誤頁面</title>
</head>
<body>
出錯了,請與管理員聯(lián)系。。。錯誤提示頁面-ArithmeticException<br>
<span th:text="${error}"></span>
</body>
</html>
<!DOCTYPE html>
<html lang="en" >
<head><meta charset="UTF-8"><title>錯誤頁面</title>
</head>
<body>
出錯了,請與管理員聯(lián)系。。。錯誤提示頁面-NullPointerException <br>
<span th:text="${error}"></span>
</body>
</html>
?
?@ControllerAdvice+@ExceptionHandler處理
第二種處理方式中,異常處理的代碼和業(yè)務(wù)代碼放在一個類中了,這種方式耦合性太強了,最好是將業(yè)務(wù)和異常處理的代碼分離開,這時我們可以定義一個專門的異常處理類,通過注解@ControllerAdvice來實現(xiàn)。具體如下:
@ControllerAdvice
public class GlobalException {/*** java.lang.ArithmeticException* 該方法需要返回一個 ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定* 參數(shù) Exception e:會將產(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* 該方法需要返回一個 ModelAndView:目的是可以讓我們封裝異常信息以及視圖的指定* 參數(shù) Exception e:會將產(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;}
}
控制器中實現(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將具體的異常和錯誤頁面指定對應(yīng)關(guān)系,這樣就不用每個異常都單獨寫一個方法了。
@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處理
最后我們還可以通過實現(xiàn)HandlerExceptionResolver 接口來根據(jù)不同異常類型來動態(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;}
}