SpringBoot實現全局異常處理總結
SpringBoot實現全局異常處理
在項目開發中出現異常時很平常不過的事情,我們處理異常也有很多種方式,可能如下:
public int div(int a ,int b){
int c=0;
try{
c=a/b;
}catch (Exception ex){
ex.printStackTrace();
}
return c;
}
如果我們這樣處理異常,代碼中就會出現特別多的異常處理模塊,這樣代碼就會變得可讀性非常差,而且業務模塊邏輯會夾雜特別多的非業務邏輯。但是在項目開發的過程中我們應該將主要精力放在業務模塊,除了必要的異常處理模塊最好不要再包含其他無關緊要的代碼。那么我們如何處理項目中無處不在的異常呢?這就引出了我們要介紹的全局異常處理方法,主要有兩種種方式:
- HandlerExceptionResolver。
- @ControllerAdvice+@ExceptionHandler 今天我們主要介紹一下@ControllerAdvice+@ExceptionHandler模式處理全局異常。
全局異常處理
首先我們先介紹一下@ControllerAdvice和@ExceptionHandler
- @ControllerAdvice注解:他是一個比較特殊的@Component,用于定義全局異常處理類作用在所有的@Controller類型的接口上。
- @ExceptionHandler注解:用于聲明處理異常的方法。
配置全局異常
@ControllerAdvice+@ExceptionHandler只要設計得當,就不用再在Controller使用trg-catch了!下面我們先寫介紹一個Controller層全局異常處理類。
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(Exception.class)
public CommonResult exceptionHandler(HttpServletRequest request, Exception exception) throws Exception {
Map<String, Object> result = new HashMap<>(3);
String message =exception.getMessage()+request.getRequestURL().toString();
return CommonResult.failed(message);
}
}
注:@ResponseBody的作用其實是將java對象轉為json格式的數據。然后到這里為止,一個簡單的全局異常處理解決方式就完成了,這只是一個簡單的異常處理方式,遠遠不能達到完整項目中全局異常處理的方案。
全局異常處理的升級
我們項目中業務處理,可以通過自定義的異常知道哪一個模塊發生異常,并且不同的業務模塊也有不同的異常處理方式,這也方便我們做擴展
public class ServiceException extends RuntimeException {
private IErrorCode errorCode;
public ServiceException(IErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}
public ServiceException(String message) {
super(message);
}
public ServiceException(Throwable cause) {
super(cause);
}
public ServiceException(String message, Throwable cause) {
super(message, cause);
}
public IErrorCode getErrorCode() {
return errorCode;
}
}
加入自定義異常處理
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 處理所有Service層異常
*/
@ResponseBody
@ExceptionHandler(value = ServiceException.class)
public CommonResult handle(ServiceException e) {
if (e.getErrorCode() != null) {
return CommonResult.failed(e.getErrorCode());
}
return CommonResult.failed(e.getMessage());
}
/**
* 處理所有不可知的異常
*/
@ResponseBody
@ExceptionHandler(Exception.class)
public CommonResult exceptionHandler(HttpServletRequest request, Exception exception) throws Exception {
Map<String, Object> result = new HashMap<>(3);
String message =exception.getMessage()+request.getRequestURL().toString();
return CommonResult.failed(message);
}
}
處理 Controller 數據綁定、數據校驗的異常
在用戶登錄Model字段上注解數據校驗規則。
@Data
@EqualsAndHashCode(callSuper = false)
public class UserLoginParam {
@NotEmpty
private String username;
@NotEmpty
private String password;
}
SpringBoot中可以使用@Validated + @RequestBody注解方式實現數據綁定和數據校驗。例如登錄方式為:
@ApiOperation(value = "登錄以后返回token")
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public CommonResult login(@Validated @RequestBody UmsAdminLoginParam umsAdminLoginParam) {
String token = adminService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
if (token == null) {
return CommonResult.validateFailed("用戶名或密碼錯誤");
}
Map<String, String> tokenMap = new HashMap<>();
tokenMap.put("token", token);
tokenMap.put("tokenHead", tokenHead);
return CommonResult.success(tokenMap);
}
如果數據校驗不對數據拋出的異常為MethodArgumentNotValidException,所以我們可以在全局異常處理類中添加對MethodArgumentNotValidException異常的處理聲明,就可以實現全局處理數據校驗和綁定的異常了,實現如下:
@ResponseBody
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public CommonResult handleValidException(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
String message = null;
if (bindingResult.hasErrors()) {
FieldError fieldError = bindingResult.getFieldError();
if (fieldError != null) {
message = fieldError.getField()+fieldError.getDefaultMessage();
}
}
return CommonResult.validateFailed(message);
}
通過上面介紹的未知異常、數據校驗和自定義全局異常所有的Controller層的異常處理方式全部都集中到了GlobalExceptionHandler類中,那么我們在Controller類中就不再需要收到記錄錯誤了。
GlobalExceptionHandler全部代碼
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(value = ApiException.class)
public CommonResult handle(ApiException e) {
if (e.getErrorCode() != null) {
return CommonResult.failed(e.getErrorCode());
}
return CommonResult.failed(e.getMessage());
}
@ResponseBody
@ExceptionHandler(Exception.class)
public CommonResult exceptionHandler(HttpServletRequest request, Exception exception) throws Exception {
Map<String, Object> result = new HashMap<>(3);
String message =exception.getMessage()+request.getRequestURL().toString();
return CommonResult.failed(message);
// return result;
}
@ResponseBody
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public CommonResult handleValidException(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
String message = null;
if (bindingResult.hasErrors()) {
FieldError fieldError = bindingResult.getFieldError();
if (fieldError != null) {
message = fieldError.getField()+fieldError.getDefaultMessage();
}
}
return CommonResult.validateFailed(message);
}
}
總結
今天主要講解了@ControllerAdvice+@ExceptionHandler進行統一的在Controller層上的全局異常處理。