spring boot配置統一異常處理

基於@ControllerAdvice的統一異常處理web

>.這裏ServerException是我自定義的異常,和普通Exception分開處理spring

>.這裏的RequestResult是我自定義的請求返回結果對象api

ExceptionResolver.class

import com.dawn.blogspot.common.exception.ServerException;
import com.dawn.blogspot.common.response.RequestResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author TangZedong
 * @apiNote 統一異常處理器
 * @since 2018/5/21 11:55
 */
@ControllerAdvice
public class ExceptionResolver {
// spring boot 2.0.4版本內部集成了log4j,因此不須要額外的配置log4j
private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionResolver.class); /** * 處理自定義異常{@link ServerException},並返回錯誤信息 */ @ResponseBody @ExceptionHandler(value = ServerException.class) public RequestResult ServerException(ServerException e) { return RequestResult.getFailedInstance(e.getCode(), e.getMessage() == null ? "系統異常" : e.getMessage()); } /** * 處理自定義異常{@link ServerException},並返回錯誤信息 */ @ResponseBody @ExceptionHandler(value = Exception.class) public RequestResult ServerException() { return RequestResult.getFailedInstance("系統異常"); } }

ServerException.class

/**
 * @author TangZedong
 * @apiNote 服務異常
 * @since 2018/9/4 15:27
 */
public class ServerException extends RuntimeException {
    // 錯誤代碼
    private short code = -1;

    public short getCode() {
        return code;
    }

    public ServerException(String message) {
        super(message);
    }

    public ServerException(short code, String message) {
        super(message);
        this.code = code;
    }

    public ServerException(short code, String message, Throwable t) {
        super(message, t);
        this.code = code;
    }

}

RequestResult.class

/**
 * @author TangZedong
 * @apiNote 請求結果
 * @since 2018/9/4 16:20
 */
public class RequestResult<T> {
    private static final short SUCCESS_CODE = 0;
    private static final short FAILED_CODE = -1;

    private static final boolean SUCCESS_STATUS = true;
    private static final boolean FAILED_STATUS = false;

    private static final String NULL_MESSAGE = null;
    private static final String NULL_DATA = null;

    private short code;
    private String message;
    private boolean success;
    private T data;

    // 構造方法
    public RequestResult(short code, String message, boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    // get方法
    public short getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public boolean isSuccess() {
        return success;
    }

    // 獲取實例
    public static <T> RequestResult getFailedInstance(short code, String message, T data) {
        return new RequestResult(code, message, FAILED_STATUS, data);
    }

    public static <T> RequestResult getFailedInstance(short code, String message) {
        return new RequestResult(code, message, FAILED_STATUS, NULL_DATA);
    }

    public static <T> RequestResult getFailedInstance(String message) {
        return new RequestResult(FAILED_CODE, message, FAILED_STATUS, NULL_DATA);
    }

    public static <T> RequestResult getSuccessInstance(short code, String message, T data) {
        return new RequestResult(code, message, SUCCESS_STATUS, data);
    }

    public static <T> RequestResult getSuccessInstance(short code, T data) {
        return new RequestResult(code, NULL_MESSAGE, SUCCESS_STATUS, data);
    }

    public static <T> RequestResult getSuccessInstance(T data) {
        return new RequestResult(SUCCESS_CODE, NULL_MESSAGE, SUCCESS_STATUS, data);
    }

}
相關文章
相關標籤/搜索