SpringBoot定义全局统一业务异常

定义统一异常类:BusinessException.java

package com.miaoying;

import lombok.Data;

@Data
public class BusinessException extends RuntimeException {

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String message;

    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        BusinessException that = (BusinessException) o;

        if (!code.equals(that.code)) return false;
        return message.equals(that.message);

    }

    @Override
    public int hashCode() {
        int result = code != null ? code.hashCode() : 0;
        result = 31 * result + (message != null ? message.hashCode() : 0);
        return result;
    }
}

定义统一的数据返回的结构:ResultResponse<T>.java

package com.miaoying;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultResponse<T> implements Serializable {

    private static final long serialVersionUID = 5261844042861308860L;
    private int retCode;

    private String retMsg;

    private T rsp;

    public static <T> ResultResponse<T> wrapSuccessfulResult(T data) {
        ResultResponse<T> result = new ResultResponse<T>();
        result.rsp = data;
        result.retCode = 200;
        result.retMsg = "ok";
        return result;
    }
}

定义异常拦截器:BusinessExceptionFilter.java

package com.miaoying

import com.miaoying.BusinessException;
import com.miaoying.ResultResponse;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@Slf4j
public class BusinessExceptionFilter {

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Object handleBusinessException(HttpServletRequest request, BusinessException ex) {
        log.error("BusinessException code:{},msg:{}", ex.getCode(), ex.getMessage());
        ResultResponse response = new ResultResponse(ex.getCode(), ex.getMessage(), null);
        return JSONObject.toJSON(response);
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(HttpServletRequest request, Exception ex) {
        log.error("Exception code:{},msg:{}", 500, ex.getMessage());
        ResultResponse response = new ResultResponse(500, "目前业务繁忙,请您稍后!", null);
        return JSONObject.toJSON(response);
    }
}

在需要自定义异常的地方可以直接以如下形式抛出:

throw new BusinessException(503, "异常测试");

可以将错误码、错误信息统一管理成常量,总之,还有优化的空间~

猜你喜欢

转载自www.cnblogs.com/miaoying/p/12305624.html
今日推荐