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

    我不作恶

    但有权拒绝为善

    我不赞同

    但是我捍卫你不为善的权力

  • 相关阅读:
    总结
    spring boot 使用mongodb基本操作与一些坑
    java 在循环中删除数组元素之二
    学习spring cloud 笔记
    一些名词解释
    redis--分布式锁
    微信小程序的加密与解密--java
    java 动态代理
    (收藏)CORS(跨域资源共享)
    策略模式学习笔记--在写更好的代码路上
  • 原文地址:https://www.cnblogs.com/HackerBlog/p/9585641.html
Copyright © 2020-2023  润新知