SpringBoot實(shí)現(xiàn)全局異常處理總結(jié)
SpringBoot實(shí)現(xiàn)全局異常處理
在項(xiàng)目開發(fā)中出現(xiàn)異常時(shí)很平常不過的事情,我們處理異常也有很多種方式,可能如下:
public int div(int a ,int b){
int c=0;
try{
c=a/b;
}catch (Exception ex){
ex.printStackTrace();
}
return c;
}
如果我們這樣處理異常,代碼中就會(huì)出現(xiàn)特別多的異常處理模塊,這樣代碼就會(huì)變得可讀性非常差,而且業(yè)務(wù)模塊邏輯會(huì)夾雜特別多的非業(yè)務(wù)邏輯。但是在項(xiàng)目開發(fā)的過程中我們應(yīng)該將主要精力放在業(yè)務(wù)模塊,除了必要的異常處理模塊最好不要再包含其他無關(guān)緊要的代碼。那么我們?nèi)绾翁幚眄?xiàng)目中無處不在的異常呢?這就引出了我們要介紹的全局異常處理方法,主要有兩種種方式:
- HandlerExceptionResolver。
- @ControllerAdvice+@ExceptionHandler 今天我們主要介紹一下@ControllerAdvice+@ExceptionHandler模式處理全局異常。
全局異常處理
首先我們先介紹一下@ControllerAdvice和@ExceptionHandler
- @ControllerAdvice注解:他是一個(gè)比較特殊的@Component,用于定義全局異常處理類作用在所有的@Controller類型的接口上。
- @ExceptionHandler注解:用于聲明處理異常的方法。
配置全局異常
@ControllerAdvice+@ExceptionHandler只要設(shè)計(jì)得當(dāng),就不用再在Controller使用trg-catch了!下面我們先寫介紹一個(gè)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的作用其實(shí)是將java對(duì)象轉(zhuǎn)為json格式的數(shù)據(jù)。然后到這里為止,一個(gè)簡(jiǎn)單的全局異常處理解決方式就完成了,這只是一個(gè)簡(jiǎn)單的異常處理方式,遠(yuǎn)遠(yuǎn)不能達(dá)到完整項(xiàng)目中全局異常處理的方案。
全局異常處理的升級(jí)
我們項(xiàng)目中業(yè)務(wù)處理,可以通過自定義的異常知道哪一個(gè)模塊發(fā)生異常,并且不同的業(yè)務(wù)模塊也有不同的異常處理方式,這也方便我們做擴(kuò)展
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 數(shù)據(jù)綁定、數(shù)據(jù)校驗(yàn)的異常
在用戶登錄Model字段上注解數(shù)據(jù)校驗(yàn)規(guī)則。
@Data
@EqualsAndHashCode(callSuper = false)
public class UserLoginParam {
@NotEmpty
private String username;
@NotEmpty
private String password;
}
SpringBoot中可以使用@Validated + @RequestBody注解方式實(shí)現(xiàn)數(shù)據(jù)綁定和數(shù)據(jù)校驗(yàn)。例如登錄方式為:
@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("用戶名或密碼錯(cuò)誤");
}
Map<String, String> tokenMap = new HashMap<>();
tokenMap.put("token", token);
tokenMap.put("tokenHead", tokenHead);
return CommonResult.success(tokenMap);
}
如果數(shù)據(jù)校驗(yàn)不對(duì)數(shù)據(jù)拋出的異常為MethodArgumentNotValidException,所以我們可以在全局異常處理類中添加對(duì)MethodArgumentNotValidException異常的處理聲明,就可以實(shí)現(xiàn)全局處理數(shù)據(jù)校驗(yàn)和綁定的異常了,實(shí)現(xiàn)如下:
@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);
}
通過上面介紹的未知異常、數(shù)據(jù)校驗(yàn)和自定義全局異常所有的Controller層的異常處理方式全部都集中到了GlobalExceptionHandler類中,那么我們?cè)贑ontroller類中就不再需要收到記錄錯(cuò)誤了。
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);
}
}
總結(jié)
今天主要講解了@ControllerAdvice+@ExceptionHandler進(jìn)行統(tǒng)一的在Controller層上的全局異常處理。