轉自:https://www.cnblogs.com/junzi2099/p/7840294.html
Spring MVC異常統一處理的三種方式
Spring 統一異常處理有 3 種方式,分別爲:
- 使用 @ ExceptionHandler 註解
- 實現 HandlerExceptionResolver 接口
- 使用 @ControllerAdvice+ @ ExceptionHandler (推薦)
使用 @ ExceptionHandler 註解
使用該註解有一個不好的地方就是:進行異常處理的方法必須與出錯的方法在同一個Controller裏面。使用如下:
@Controller
public class GlobalController {
/**
* 用於處理異常的
* @return
*/
@ExceptionHandler({MyException.class})
public String exception(MyException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return "exception";
}
@RequestMapping("test")
public void test() {
throw new MyException("出錯了!");
}
}
可以看到,這種方式最大的缺陷就是不能全局控制異常。每個類都要寫一遍。
實現 HandlerExceptionResolver 接口
@Component
public class ExceptionTest implements HandlerExceptionResolver{
/**
* TODO 簡單描述該方法的實現功能(可選).
* @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
*/
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
System.out.println("This is exception handler method!");
return null;
}
}
使用 @ControllerAdvice+ @ ExceptionHandler 註解
上文說到 @ ExceptionHandler 需要進行異常處理的方法必須與出錯的方法在同一個Controller裏面。那麼當代碼加入了 @ControllerAdvice,則不需要必須在同一個 controller 中了。這也是 Spring 3.2 帶來的新特性。從名字上可以看出大體意思是控制器增強。 也就是說,@ControllerAdvice + @ ExceptionHandler 也可以實現全局的異常捕捉,通過@ResponseStatus(HttpStatus.XXX)返回狀態碼,非常適合處理rest接口異常。
請確保此WebExceptionHandle 類能被掃描到並裝載進 Spring 容器中。
@ControllerAdvice
@ResponseBody
public class WebExceptionHandle {
private static Logger logger = LoggerFactory.getLogger(WebExceptionHandle.class);
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public ServiceResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
logger.error("參數解析失敗", e);
return ServiceResponseHandle.failed("could_not_read_json");
}
/**
* 405 - Method Not Allowed
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ServiceResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
logger.error("不支持當前請求方法", e);
return ServiceResponseHandle.failed("request_method_not_supported");
}
/**
* 415 - Unsupported Media Type
*/
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ServiceResponse handleHttpMediaTypeNotSupportedException(Exception e) {
logger.error("不支持當前媒體類型", e);
return ServiceResponseHandle.failed("content_type_not_supported");
}
/**
* 500 - Internal Server Error
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ServiceResponse handleException(Exception e) {
if (e instanceof BusinessException){
return ServiceResponseHandle.failed("BUSINESS_ERROR", e.getMessage());
}
logger.error("服務運行異常", e);
e.printStackTrace();
return ServiceResponseHandle.failed("server_error");
}
}
如果 @ExceptionHandler 註解中未聲明要處理的異常類型,則默認爲參數列表中的異常類型。所以還可以寫成這樣:
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler()
@ResponseBody
String handleException(Exception e){
return "Exception Deal! " + e.getMessage();
}
}
參數對象就是 Controller 層拋出的異常對象!
拓展:
繼承 ResponseEntityExceptionHandler 類來實現針對 Rest 接口 的全局異常捕獲,並且可以返回自定義格式:
@Slf4j
@ControllerAdvice
public class ExceptionHandlerBean extends ResponseEntityExceptionHandler {
/**
* 數據找不到異常
* @param ex
* @param request
* @return
* @throws IOException
*/
@ExceptionHandler({DataNotFoundException.class})
public ResponseEntity<Object> handleDataNotFoundException(RuntimeException ex, WebRequest request) throws IOException {
return getResponseEntity(ex,request,ReturnStatusCode.DataNotFoundException);
}
/**
* 根據各種異常構建 ResponseEntity 實體. 服務於以上各種異常
* @param ex
* @param request
* @param specificException
* @return
*/
private ResponseEntity<Object> getResponseEntity(RuntimeException ex, WebRequest request, ReturnStatusCode specificException) {
ReturnTemplate returnTemplate = new ReturnTemplate();
returnTemplate.setStatusCode(specificException);
returnTemplate.setErrorMsg(ex.getMessage());
return handleExceptionInternal(ex, returnTemplate,
new HttpHeaders(), HttpStatus.OK, request);
}
}