spring boot配置统一异常处理

spring boot配置统一异常处理

基于@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);
    }

}

 

posted @ 2018-09-04 17:13 dawn-tangzedong 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值