SpringBoot中全局异常处理

参考:这里

统一返回UI格式类型

 1 public class CommonReturnType {
 2     //返回给UI的数据
 3     private Object data;
 4     //返回给UI的状态
 5     private String status;
 6 
 7     public static CommonReturnType create(Object result){
 8         //默认返回状态为success
 9         return CommonReturnType.create(result,"success");
10     }
11     public static CommonReturnType create(Object result,String status){
12         CommonReturnType type = new CommonReturnType();
13         type.setData(result);
14         type.setStatus(status);
15         return type;
16     }
17 
18     public Object getData() {
19         return data;
20     }
21 
22     public void setData(Object data) {
23         this.data = data;
24     }
25 
26     public String getStatus() {
27         return status;
28     }
29 
30     public void setStatus(String status) {
31         this.status = status;
32     }
33 }

自定义异常格式

1 public interface CommonError {
2     public int getErrCode();
3     public String getErrMsg();
4     public CommonError setErrMsg(String errMsg);
5 }
1 public enum EmBusinessError implements CommonError{
2     //10000开头为通用错误类型
3     PARAMETER_VALIDATION_ERROR(10001,"参数不合法"),
4 
5     //未知错误
6     UNKNOW_ERROR(10002,"未知错误"),
7 
8     //20000开头为用户信息相关错误
9     USER_NOT_EXIST(20001,"用户不存在"),
10     User_LOGIN_ERROR(20002,"手机号或密码错误"),
11     User_NOT_LOGIN(20003,"用户未登陆"),
12 
13     //30000开头为交易信息异常
14     STOCK_NOT_ENOUGH(30001,"库存不足")
15     ;
16     private int errCode;
17     private String errMsg;
18 
19     EmBusinessError(int errCode, String errMsg) {
20         this.errCode = errCode;
21         this.errMsg = errMsg;
22     }
23 
24     @Override
25     public int getErrCode() {
26         return this.errCode;
27     }
28 
29     @Override
30     public String getErrMsg() {
31         return this.errMsg;
32     }
33 
34     @Override
35     public CommonError setErrMsg(String errMsg) {
36         this.errMsg = errMsg;
37         return this;
38     }
39 }
 1 public class BusinessException extends Exception implements CommonError {
 2     private CommonError commonError;
 3 
 4     //直接接收EmBussinessError的传参用于构造业务异常
 5     public BusinessException(CommonError commonError){
 6         super();
 7         this.commonError=commonError;
 8     }
 9 
10     //接受自定义errMsg方式构造业务异常
11     public BusinessException(CommonError commonError,String errMsg){
12         super();
13         this.commonError=commonError;
14         this.commonError.setErrMsg(errMsg);
15     }
16 
17     @Override
18     public int getErrCode() {
19         return this.commonError.getErrCode();
20     }
21 
22     @Override
23     public String getErrMsg() {
24         return this.commonError.getErrMsg();
25     }
26 
27     @Override
28     public CommonError setErrMsg(String errMsg) {
29         return this.commonError.setErrMsg(errMsg);
30     }
31 }

全局异常处理器

 1 import com.wu.error.BusinessException;
 2 import com.wu.error.EmBusinessError;
 3 import com.wu.response.CommonReturnType;
 4 import org.springframework.web.bind.ServletRequestBindingException;
 5 import org.springframework.web.bind.annotation.ControllerAdvice;
 6 import org.springframework.web.bind.annotation.ExceptionHandler;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 import org.springframework.web.servlet.NoHandlerFoundException;
 9 
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import java.util.HashMap;
13 import java.util.Map;
14 
15 @ControllerAdvice
16 public class GlobalExceptionHandler{
17     @ExceptionHandler(Exception.class)
18     @ResponseBody
19     public CommonReturnType doError(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Exception ex) {
20         ex.printStackTrace();
21         Map<String,Object> responseData = new HashMap<>();
22         if( ex instanceof BusinessException){
23             BusinessException businessException = (BusinessException)ex;
24             responseData.put("errCode",businessException.getErrCode());
25             responseData.put("errMsg",businessException.getErrMsg());
26         }else if(ex instanceof ServletRequestBindingException){
27             responseData.put("errCode", EmBusinessError.UNKNOW_ERROR.getErrCode());
28             responseData.put("errMsg","url绑定路由问题");
29         }else{
30             responseData.put("errCode", EmBusinessError.UNKNOW_ERROR.getErrCode());
31             responseData.put("errMsg",EmBusinessError.UNKNOW_ERROR.getErrMsg());
32         }
33         return CommonReturnType.create(responseData,"fail");
34     }
35 }