詳解SpringMVC中的異常處理
1. SpringMVC默認(rèn)三個異常處理類
- ExceptionHandlerExceptionResolver:處理@ExceptionHandler注解
- ResponseStatusExceptionResolver:處理@ResponseStatus注解
- DefaultHandlerExceptionResolver:處理SpringMVC自帶的異常
如果以上3個異常解析器都無法處理,會上拋給tomcat,處理異常內(nèi)部的默認(rèn)工作流程:所有異常解析器依次嘗試解析,解析完成進(jìn)行后續(xù)操作,解析失敗,下一個解析器繼續(xù)嘗試解析。
2. @ExceptionHandler注解異常
@ExceptionHandler標(biāo)注在方法上
- 方法上寫一個Exception用來接收發(fā)生的異常。
- 要攜帶異常信息不能給參數(shù)位置寫Model,正確的做法是返回ModelAndView。
- 如果有多個@ExceptionHandler都能處理這個異常,精確優(yōu)先。
@ExceptionHandler(value = { ArithmeticException.class, NullPointerException.class }) // 告訴SpringMVC,這個方法專門處理這個類發(fā)送的所有異常 public ModelAndView handleException01(Exception exception) { System.out.println("handleException01..." + exception); ModelAndView view = new ModelAndView("myerror"); view.addObject("ex", exception); return view; }
@ExceptionHandler統(tǒng)一異常處理
- 將@ExceptionHandler放在一個單獨的類中,進(jìn)行全局異常處理
- 統(tǒng)一異常管理類需要通過@ControllerAdvice注解加入IoC容器中
- 全局異常處理與本類異常處理同時存在,本類優(yōu)先
@ControllerAdvice public class MyException { // 處理空指針異常 @ExceptionHandler(value = { NullPointerException.class }) public ModelAndView handleException01(Exception exception) { System.out.println("全局的handleException01..." + exception); ModelAndView view = new ModelAndView("myerror"); view.addObject("ex", exception); return view; } // 處理算數(shù)異常 @ExceptionHandler(value = { ArithmeticException.class }) public ModelAndView handleException02(Exception exception) { System.out.println("全局的handleException02..." + exception); ModelAndView view = new ModelAndView("myerror"); view.addObject("ex", exception); return view; } }
3. @ResponseStatus注解異常
@ResponseStatus注解標(biāo)注在自定義異常上,用于設(shè)置自定義異常信息
@ResponseStatus(reason = "用戶被拒絕登錄", value = HttpStatus.NOT_ACCEPTABLE) public class UsernameNotFoundException extends RuntimeException { private static final long serialVersionUID = 1L; }
4. DefaultHandlerExceptionResolver默認(rèn)異常
DefaultHandlerExceptionResolver包含以下默認(rèn)異常
源碼: public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionResolver { ... @Override protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { try { if (ex instanceof NoSuchRequestHandlingMethodException) { return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, request, response, handler); } else if (ex instanceof HttpRequestMethodNotSupportedException) { return handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException) ex, request, response, handler); } else if (ex instanceof HttpMediaTypeNotSupportedException) { return handleHttpMediaTypeNotSupported((HttpMediaTypeNotSupportedException) ex, request, response, handler); } else if (ex instanceof HttpMediaTypeNotAcceptableException) { return handleHttpMediaTypeNotAcceptable((HttpMediaTypeNotAcceptableException) ex, request, response, handler); } else if (ex instanceof MissingPathVariableException) { return handleMissingPathVariable((MissingPathVariableException) ex, request, response, handler); } else if (ex instanceof MissingServletRequestParameterException) { return handleMissingServletRequestParameter((MissingServletRequestParameterException) ex, request, response, handler); } else if (ex instanceof ServletRequestBindingException) { return handleServletRequestBindingException((ServletRequestBindingException) ex, request, response, handler); } else if (ex instanceof ConversionNotSupportedException) { return handleConversionNotSupported((ConversionNotSupportedException) ex, request, response, handler); } else if (ex instanceof TypeMismatchException) { return handleTypeMismatch((TypeMismatchException) ex, request, response, handler); } else if (ex instanceof HttpMessageNotReadableException) { return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, request, response, handler); } else if (ex instanceof HttpMessageNotWritableException) { return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler); } else if (ex instanceof MethodArgumentNotValidException) { return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response, handler); } else if (ex instanceof MissingServletRequestPartException) { return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request, response, handler); } else if (ex instanceof BindException) { return handleBindException((BindException) ex, request, response, handler); } else if (ex instanceof NoHandlerFoundException) { return handleNoHandlerFoundException((NoHandlerFoundException) ex, request, response, handler); } } catch (Exception handlerException) { if (logger.isWarnEnabled()) { logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException); } } return null; } ... }
如下HttpRequestMethodNotSupportedException請求方式不對。使用GET對POST方法進(jìn)行訪問
@RequestMapping(value = "/handle03", method = RequestMethod.POST) public String postMethod() { return "success"; }
5. 沒有找到對應(yīng)異常處理類
若沒有對應(yīng)異常處理類,會進(jìn)入對應(yīng)服務(wù)器錯誤頁面
以上就是詳解SpringMVC中的異常處理的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC 異常處理的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
idea指定maven的settings文件不生效的問題解決
本文主要介紹了idea指定maven的settings文件不生效的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06IDEA調(diào)試功能使用總結(jié)(step?over/step?into/force?step?into/step?o
本文主要介紹了IDEA調(diào)試功能使用總結(jié)(step?over/step?into/force?step?into/step?out),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07SpringBoot+Quartz+數(shù)據(jù)庫存儲的完美集合
這篇文章主要介紹了SpringBoot+Quartz+數(shù)據(jù)庫存儲的示例代碼,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02SpringBoot多環(huán)境配置及配置文件分類實例詳解
這篇文章主要介紹了SpringBoot多環(huán)境配置及配置文件分類,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10