SpringBoot全局異常捕獲處理及參數(shù)校驗
點擊關(guān)注公眾號,Java干貨及時送達(dá)
牛逼!又發(fā)現(xiàn)了一款面試題庫,太全了!!
(點擊查看)
一,為什么要用全局異常處理?
在日常開發(fā)中,為了不拋出異常堆棧信息給前端頁面,每次編寫Controller層代碼都要盡可能的catch住所有service層、dao層等異常,代碼耦合性較高,且不美觀,不利于后期維護。
二,應(yīng)用場景是什么?
非常方便的去掉了try catch這類冗雜難看的代碼,有利于代碼的整潔和優(yōu)雅 自定義參數(shù)校驗時候全局異常處理會捕獲異常,將該異常統(tǒng)一返回給前端,省略很多if else代碼 當(dāng)后端出現(xiàn)異常時,需要返回給前端一個友好的界面的時候就需要全局異常處理 因為異常時層層向上拋出的,為了避免控制臺打印一長串異常信息
三、如何進行全局異常捕獲和處理?
一共有兩種方法:
Spring的AOP(較復(fù)雜) @ControllerAdvice結(jié)合@ExceptionHandler(簡單)
四、@ControllerAdvice和@ExceptionHandler怎么用?
1、Controller Advice字面上意思是“控制器通知”,Advice除了“勸告”、“意見”之外,還有“通知”的意思。
可以將@ExceptionHandler(標(biāo)識異常類型對應(yīng)的處理方法)標(biāo)記的方法提取出來,放到一個類里,并將加上@ControllerAdvice,這樣,所有的控制器都可以用了
@ControllerAdvice
public class ControllerHandlers(){
@ExceptionHandler
public String errorHandler(Exception e){
return "error";
}
}
2、 因為@ControllerAdvice被@Componen標(biāo)記,所以他可以被組件掃描到并放入Spring容器
3、 如果只想對一部分控制器通知,比如某個包下邊的控制器,就可以這樣寫:
@ControllerAdvice("com.labor")
public class ControllerHandlers(){}
也可以直接寫類名
@ControllerAdvice(basePackageClasses = ***.class)
public class ControllerHandlers(){}
也可以傳多個類
@ControllerAdvice(assignableTypes = {***.class,***.class})
public class ControllerHandlers(){}
4、 控制器通知還有一個兄弟,@RestControllerAdvice,如果用了它,錯誤處理方法的返回值不會表示用的哪個視圖,而是會作為HTTP body處理,即相當(dāng)于錯誤處理方法加了@ResponseBody注解。
@RestControllerAdvice
public class ControllerHandlers(){
@ExceptionHandler
public String errorHandler(Exception e){
return "error";
}
}
5、@ExceptionHandler注解的方法只能返回一種類型,在前后端分離開發(fā)中我們通常返回,統(tǒng)一返回類型和優(yōu)化錯誤的提示,我們可以封裝我們自己的返回Map
public class AjaxResult extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public static final String CODE_TAG = "code";
public static final String MSG_TAG = "msg";
public static final String DATA_TAG = "data";
/**
* 狀態(tài)類型
*/
public enum Type {
/**
* 成功
*/
SUCCESS(1),
/**
* 警告
*/
WARN(2),
/**
* 錯誤
*/
ERROR(0),
/**無權(quán)限*/
UNAUTH(3),
/**未登錄、登錄超時*/
UNLOGIN(4);
private final int value;
Type(int value) {
this.value = value;
}
public int value() {
return this.value;
}
}
/**
* 狀態(tài)類型
*/
private Type type;
/**
* 狀態(tài)碼
*/
private int code;
/**
* 返回內(nèi)容
*/
private String msg;
/**
* 數(shù)據(jù)對象
*/
private Object data;
/**
* 初始化一個新創(chuàng)建的 AjaxResult 對象,使其表示一個空消息。
*/
public AjaxResult() {
}
/**
* 初始化一個新創(chuàng)建的 AjaxResult 對象
* @param type 狀態(tài)類型
* @param msg 返回內(nèi)容
*/
public AjaxResult(Type type, String msg) {
super.put(CODE_TAG, type.value);
super.put(MSG_TAG, msg);
}
/**
* 初始化一個新創(chuàng)建的 AjaxResult 對象
* @param type 狀態(tài)類型
* @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
*/
public AjaxResult(Type type, String msg, Object data) {
super.put(CODE_TAG, type.value);
super.put(MSG_TAG, msg);
/* 數(shù)據(jù)為空的時候,還是需要把參數(shù)傳給前臺 huangqr @2019.7.19
if (StringUtils.isNotNull(data)) {
super.put(DATA_TAG, data);
}*/
super.put(DATA_TAG, data);
}
/**
* 返回成功消息
* @return 成功消息
*/
public static AjaxResult success() {
return AjaxResult.success("操作成功");
}
/**
* 返回成功數(shù)據(jù)
* @return 成功消息
*/
public static AjaxResult success(Object data) {
return AjaxResult.success("操作成功", data);
}
/**
* 返回成功消息
* @param msg 返回內(nèi)容
* @return 成功消息
*/
public static AjaxResult success(String msg) {
return AjaxResult.success(msg, null);
}
/**
* 返回成功消息
* @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data) {
return new AjaxResult(Type.SUCCESS, msg, data);
}
/**
* 返回警告消息
* @param msg 返回內(nèi)容
* @return 警告消息
*/
public static AjaxResult warn(String msg) {
return AjaxResult.warn(msg, null);
}
/**
* 返回警告消息
* @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
* @return 警告消息
*/
public static AjaxResult warn(String msg, Object data) {
return new AjaxResult(Type.WARN, msg, data);
}
/**
* 返回錯誤消息
* @return
*/
public static AjaxResult error() {
return AjaxResult.error("操作失敗");
}
/**
* 返回錯誤消息
* @param msg 返回內(nèi)容
* @return 警告消息
*/
public static AjaxResult error(String msg) {
return AjaxResult.error(msg, null);
}
/**
* 返回錯誤消息
* @param msg 返回內(nèi)容
* @param data 數(shù)據(jù)對象
* @return 警告消息
*/
public static AjaxResult error(String msg, Object data) {
return new AjaxResult(Type.ERROR, msg, data);
}
/**
* 無權(quán)限返回
* @return
*/
public static AjaxResult unauth() {
return new AjaxResult(Type.UNAUTH, "您沒有訪問權(quán)限!", null);
}
/**
* 無權(quán)限
*
* @param msg
* @return com.wanda.labor.framework.web.domain.AjaxResult
* @exception
*/
public static AjaxResult unauth(String msg) {
return new AjaxResult(Type.UNAUTH, msg, null);
}
/**
* 未登錄或登錄超時。請重新登錄
*
* @param
* @return com.wanda.labor.framework.web.domain.AjaxResult
* @exception
*/
public static AjaxResult unlogin() {
return new AjaxResult(Type.UNLOGIN, "未登錄或登錄超時。請重新登錄!", null);
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static class SUCCESS{
public static AjaxResult data(Object data){
return new AjaxResult(Type.SUCCESS, "操作成功 Operation Successful", data);
}
public static AjaxResult iMessagesg(String msg){
return new AjaxResult(Type.SUCCESS, msg, null);
}
public static AjaxResult imsgAndData(String msg,Object data){
return new AjaxResult(Type.SUCCESS, msg, data);
}
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("code", getCode())
.append("msg", getMsg()).append("data", getData()).toString();
}
}
6、 完善全局異常處理器
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 權(quán)限校驗失敗 如果請求為ajax返回json,普通請求跳轉(zhuǎn)頁面
*/
@ExceptionHandler(AuthorizationException.class)
public Object handleAuthorizationException(HttpServletRequest request, AuthorizationException e) {
//log.error(e.getMessage(), e);
if (ServletUtils.isAjaxRequest(request)) {
return AjaxResult.unauth(PermissionUtils.getMsg(e.getMessage()));
} else {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("error/unauth");
return modelAndView;
}
}
/**
* 請求方式不支持
*/
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
public AjaxResult handleException(HttpRequestMethodNotSupportedException e) {
log.error(e.getMessage(), e);
return AjaxResult.error("不支持' " + e.getMethod() + "'請求");
}
/**
* 攔截未知的運行時異常
*/
@ExceptionHandler(RuntimeException.class)
public AjaxResult notFount(RuntimeException e) {
log.error("運行時異常:", e);
return AjaxResult.error("運行時異常:" + e.getMessage());
}
/**
* 系統(tǒng)異常
*/
@ExceptionHandler(Exception.class)
public AjaxResult handleException(Exception e) {
log.error(e.getMessage(), e);
return AjaxResult.error("服務(wù)器錯誤,請聯(lián)系管理員");
}
/**
* 校驗異常
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public AjaxResult exceptionHandler(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
String errorMesssage = "";
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errorMesssage += fieldError.getDefaultMessage() + "!";
}
return AjaxResult.error(errorMesssage);
}
/**
* 校驗異常
*/
@ExceptionHandler(value = BindException.class)
public AjaxResult validationExceptionHandler(BindException e) {
BindingResult bindingResult = e.getBindingResult();
String errorMesssage = "";
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errorMesssage += fieldError.getDefaultMessage() + "!";
}
return AjaxResult.error(errorMesssage);
}
/**
* 校驗異常
*/
@ExceptionHandler(value = ConstraintViolationException.class)
public AjaxResult ConstraintViolationExceptionHandler(ConstraintViolationException ex) {
Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator();
List<String> msgList = new ArrayList<>();
while (iterator.hasNext()) {
ConstraintViolation<?> cvl = iterator.next();
msgList.add(cvl.getMessageTemplate());
}
return AjaxResult.error(String.join(",",msgList));
}
/**
* 業(yè)務(wù)異常
*/
@ExceptionHandler(BusinessException.class)
public AjaxResult businessException(BusinessException e) {
log.error(e.getMessage(), e);
return AjaxResult.error(e.getMessage());
}
/**
* 演示模式異常
*/
@ExceptionHandler(DemoModeException.class)
public AjaxResult demoModeException(DemoModeException e) {
return AjaxResult.error("演示模式,不允許操作");
}
}
六、@Validated 校驗器注解的異常,也可以一起處理,無需手動判斷綁定校驗結(jié)果 BindingResult/Errors 了
pom文件引入validation的jar包。推薦:250期面試題
<!-- 校驗-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
等待校驗的object
public class Person {
/**
* @PersonName(prefix = "song"):自定義注解
*/
@NotNull
@PersonName(prefix = "song")
private String name;
@Min(value = 18)
@Max(value = 30, message = "超過30歲的不要!")
private Integer age;
}
自定義注解
https://blog.csdn.net/panchang199266/article/details/83050053
使用
/**
* 開啟校驗注解:@Valid
*/
@RestController
public class PersonController {
@PostMapping("/person")
public Person savePerson(@Valid @RequestBody Person person){
return person;
}
}
全局異常處理里有相應(yīng)的處理方法
/**
* 校驗異常
*/
@ExceptionHandler(value = BindException.class)
public AjaxResult validationExceptionHandler(BindException e) {
BindingResult bindingResult = e.getBindingResult();
String errorMesssage = "";
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errorMesssage += fieldError.getDefaultMessage() + "!";
}
return AjaxResult.error(errorMesssage);
}
被@RequestBody和@RequestParam注解的請求實體,校驗異常類是不同的
七、自定義異常以及事務(wù)回滾
public class MyException extends RuntimeException {
//這個地方不要寫exception,因為Spring是只對運行時異常進行事務(wù)回滾,
//如果拋出的是exception是不會進行事務(wù)回滾的。
}
如果是在service層里捕獲異常統(tǒng)一去處理,那為了保證事務(wù)的回滾,需要拋出RuntimeException
try {
} catch (Exception e) {
e.printStackTrace();
logger.error("發(fā)生異常");
throw new RuntimeException();
}
關(guān)于try-catch-finally中,finally的作用,finally設(shè)計之初就是為了關(guān)閉資源,如果在finally中使用return語句,會覆蓋try或者catch的返回值,最常見的就是覆蓋異常,即便catch往上拋了異常,也會被覆蓋,返回finally中return語句的返回值。
感謝閱讀,希望對你有所幫助 :)
來源:blog.csdn.net/songguopeng/article/details/98961787
如有文章對你有幫助,
歡迎關(guān)注??、點贊??、轉(zhuǎn)發(fā)??!
推薦, Java面試題庫,詳情點擊: 牛逼!又發(fā)現(xiàn)了一款牛逼的Java面試題庫,史上最強! 點擊文末“閱讀原文”可直達(dá)


