首页 > 技术文章 > spring boot配置统一异常处理

HackerBlog 2018-09-04 17:13 原文

基于@ControllerAdvice的统一异常处理

>.这里ServerException是我自定义的异常,和普通Exception分开处理

>.这里的RequestResult是我自定义的请求返回结果对象

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);
    }

}

 

推荐阅读