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

    我不作恶

    但有权拒绝为善

    我不赞同

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

  • 相关阅读:
    lucene DocValues——没有看懂
    lucene 索引文件大小分布_tim
    lucene segment会包含所有的索引文件,如tim tip等,可以认为是mini的独立索引
    sphinx源码分析总结
    sphinx索引部分源码续——过程:连接到CSphSource对应的sql数据源,通过fetch row取其中一行,然后解析出field,分词,获得wordhit,最后再加入到CSphSource的Hits里
    shinx索引部分源码分析——过程:连接到CSphSource对应的sql数据源,通过fetch row取其中一行,然后解析出field,分词,获得wordhit,最后再加入到CSphSource的Hits里
    php unset 数组陷阱
    php 引用
    网站开启gzip的方法
    php启用gzip压缩
  • 原文地址:https://www.cnblogs.com/HackerBlog/p/9585641.html
Copyright © 2020-2023  润新知