日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
SpringBoot實(shí)現(xiàn)全局異常處理總結(jié)

SpringBoot實(shí)現(xiàn)全局異常處理

  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)目中無處不在的異常呢?這就引出了我們要介紹的全局異常處理方法,主要有兩種種方式:

站在用戶的角度思考問題,與客戶深入溝通,找到波密網(wǎng)站設(shè)計(jì)與波密網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋波密地區(qū)。

  • 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 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 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 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 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層上的全局異常處理。


分享文章:SpringBoot實(shí)現(xiàn)全局異常處理總結(jié)
當(dāng)前路徑:http://m.5511xx.com/article/coipodo.html