SpringBoot全局异常统一处理

1.项目demo地址

链接:https://pan.baidu.com/s/1U-AKOb69J0J-pbzTLaSMHw 
提取码:cbej

2.定义统一返回值

@JsonInclude(JsonInclude.Include.NON_NULL) //null值不显示
public class ResultVO  implements Serializable {
    
    private static final String RESULT_SUCCESS = "00000000";
    private static final long serialVersionUID = 1L;

    private String resultCode = RESULT_SUCCESS;

    private String resultMsg = "SUCCESS";

    private T data;

    public ResultVO() {
    }

    public ResultVO(GlobalStatusCode globalStatusCode) {
        this(globalStatusCode, null);
    }

    public ResultVO(String statusCode, String message) {
        this(statusCode, message, null);
    }

    public ResultVO(GlobalStatusCode globalStatusCode, T data) {
        this(globalStatusCode.getStatusCode(), globalStatusCode.getMessage(), data,null);
    }

    /**
     *
     * @param globalStatusCode
     * @param data
     * @param s message 中可以存在{}占位符,通过此参数按照顺序逐个替换
     */
    public ResultVO(GlobalStatusCode globalStatusCode, T data, String... s) {
        this(globalStatusCode.getStatusCode(), globalStatusCode.getMessage(), data,s);
    }

    public ResultVO(String resultCode, String resultMsg, T data) {
        this.data = data;
        this.resultMsg = resultMsg;
        this.resultCode = resultCode;
    }

    public ResultVO(String resultCode, String resultMsg, T data, String... s) {
        this.data = data;
        this.resultMsg = resultMsg;
        this.resultCode = resultCode;
        if(s != null){
            for(String _s :s){
                this.resultMsg = this.resultMsg.replace("{}",_s);
            }
        }
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setStatusCode(GlobalStatusCode statusCode) {
        this.resultCode = statusCode.getStatusCode();
        this.resultMsg = statusCode.getMessage();
    }

    public boolean hasError() {
        return !StringUtils.equalsIgnoreCase(this.getResultCode(), RESULT_SUCCESS);
    }
}

2.定义统一异常处理类

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(value = GlobalException.class)
    @ResponseBody
    public ResultVO ErrorHandler(GlobalException e) {
        LOGGER.error("异常信息GlobalException:"+e.getMessage(), e);

        return new ResultVO(e.getStatusCode(),e.getMessage());
    }

3.定义统一异常实现runtimeExceprion

public class GlobalException extends RuntimeException {
    
    private static final long serialVersionUID = -1178522679073184111L;
    
    private String statusCode;
    private String message;
    
    
    public GlobalException() {
        super();
    }

    public GlobalException(GlobalStatusCode status) {        
        super();
        this.statusCode = status.getStatusCode();
        this.message = status.getMessage();
    }

    public GlobalException(GlobalStatusCode status, String... s) {
        super();
        this.statusCode = status.getStatusCode();
        this.message = status.getMessage();
        if(s!=null){
//            for(String _s:s){
//                this.message = this.message.replace("{}",_s);
//            }
            this.message = MessageFormatter.arrayFormat(message, s).getMessage();
        }
    }
    
    public  GlobalException(ResultVO result) {
        this(result.getResultCode(),result.getResultMsg());
    }

    public GlobalException(String statusCode, String message, String... s) {
        super();
        this.statusCode = statusCode;
        this.message = message;
        if(s!=null){
            for(String _s:s){
                this.message = this.message.replace("{}",_s);
            }
        }
    }

    public GlobalException(String statusCode, String message) {
        super();
        this.statusCode = statusCode;
        this.message = message;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }
    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

4.定义返回参数接口

public interface GlobalStatusCode {
    
    /**
     * 获取状态码
     * @return
     */
    String getStatusCode();

    /**
     * 获取消息
     * @return
     */
    String getMessage();

}

5.定义返回参数信息,实现GlobalStatusCode

public enum StatusCode implements GlobalStatusCode {

    RESULT_SUCCESS("00000000", "SUCCESS"),
    RESULT_FAIL("99999999", "请求失败"),
    PARAM_ERROR("COUPON-00000001", "传入参数错误"),
    PHONE_ERROR("COUPON-00000002", "传入手机号为空"),
    COUPON_PARAM_ERROR("COUPON-00000003", "优惠码格式错误"),
    COUPON_IS_FAILURE("COUPON-00000004", "此优惠码优惠券已失效"),
    ACCOUNT_IS_FAIL("COUPON-00000005", "用户信息处理失败"),
    VERIFY_IS_FAIL("COUPON-00000006", "参数校验失败"),
    RCS_INFO_IS_NOT_EXIST("RCSM-00010001","无此本根服务器信息");

    private String statusCode;
    private String message;

    StatusCode(String statusCode, String message) {
        this.statusCode = statusCode;
        this.message = message;
    }

    @Override
    public String getStatusCode() {
        return statusCode;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

6.测试controller

@Api(tags = "测试" ,description = "MemberController")
@RestController
@RequestMapping("/test")
public class MemberController {
    
    @ApiOperation(value = "全局异常处理测试测试数据")
    @GetMapping("/test/data")
    public ResultVO testData(int a ,int b){
        int c=a+b;
        if(b==0){
            throw new GlobalException(StatusCode.RESULT_FAIL);
        }
        if(a==1){
            throw new GlobalException(StatusCode.PARAM_ERROR);
        }
        if(a==2){
            throw new GlobalException(StatusCode.PHONE_ERROR);
        }
        if(a==3){
            throw new GlobalException(StatusCode.COUPON_PARAM_ERROR);
        }
        return new ResultVO(StatusCode.RESULT_SUCCESS);
    }

7.测试结果:

SpringBoot全局异常统一处理_第1张图片

你可能感兴趣的:(SpringBoot)