自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
今天主要講解了@ControllerAdvice+@ExceptionHandler進(jìn)行統(tǒng)一的在Controller層上的全局異常處理。

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

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2017-08-10 10:28:43

SpringBootSpring

2019-01-24 16:11:19

前端全局異常數(shù)據(jù)校驗(yàn)

2023-12-13 13:28:00

Spring全局異常處理架構(gòu)

2023-12-27 07:53:08

全局異常處理處理應(yīng)用

2009-02-06 14:11:36

ASP.NET.NET全局異常處理

2021-04-20 10:50:38

Spring Boot代碼Java

2019-08-29 14:30:16

代碼開發(fā)工具

2024-10-28 08:32:22

統(tǒng)一接口響應(yīng)SpringBoot響應(yīng)框架

2009-11-06 15:25:25

WCF異常

2016-12-15 13:31:20

Java異常處理經(jīng)驗(yàn)

2011-08-05 14:02:17

MySQL數(shù)據(jù)庫(kù)異常處理

2025-02-05 10:02:03

Locust測(cè)試異常處理

2023-06-15 14:09:00

解析器Servlet容器

2022-08-03 07:07:10

Spring數(shù)據(jù)封裝框架

2022-05-03 10:43:43

SpringJava

2023-12-11 14:19:00

Java程序異常

2023-10-27 08:20:12

springboot微服務(wù)

2013-08-22 17:10:09

.Net異常處理

2012-12-21 10:48:20

Java異常

2010-01-27 16:39:48

C++編譯器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)