zoukankan      html  css  js  c++  java
  • yb课堂之自定义异常和配置 《五》

    开发自定义异常和配置

    • 自定义异常 继承RuntimeException
    • 开发异常处理器ExceptionHandle

    YBException.java

    package net.ybclass.online_ybclass.exception;
    
    /**
     * 自定义异常类
     */
    public class YBException extends RuntimeException {
        private Integer code;
        private String msg;
    
        public YBException(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }

    CustomExceptionHandle.java

    package net.ybclass.online_ybclass.exception;
    
    import net.ybclass.online_ybclass.utils.JsonData;
    
    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;
    
    /**
     * 异常处理类
     */
    @ControllerAdvice
    public class CustomExceptionHandle {
        private final static Logger logger = LoggerFactory.getLogger(CustomExceptionHandle.class);
        //监听这个异常
        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public JsonData handle(Exception ex){
            logger.error("[ 系统异常 ]{}",ex);
            if (ex instanceof YBException){
                YBException ybException=(YBException)ex;
                return JsonData.buildError(ybException.getCode(),ybException.getMsg());
            }else {
                return JsonData.buildError("全局异常,未知从错误");
            }
        }
    }

    JsonData.java

    package net.ybclass.online_ybclass.utils;
    
    public class JsonData {
        /**
         * 状态码,0表示成功过,1表示处理中,-1表示失败
         */
        private Integer code;
        /**
         * 业务数据
         */
        private Object data;
        /**
         * 信息描述
         */
        private String msg;
    
        public JsonData() {
        }
    
        public JsonData(Integer code, Object data, String msg) {
            this.code = code;
            this.data = data;
            this.msg = msg;
        }
    
        /**
         * 成功,不用返回数据
         * @return
         */
        public static JsonData buildSuccess() {
            return new JsonData(0, null, null);
        }
    
        /**
         * 成功,返回数据
         * @param data 返回数据
         * @return
         */
        public static JsonData buildSuccess(Object data) {
            return new JsonData(0, data, null);
        }
    
        /**
         * 失败,返回信息
         * @param msg 返回信息
         * @return
         */
        public static JsonData buildError(String msg) {
            return new JsonData(-1, null, msg);
        }
    
        /**
         * 失败,返回信息和状态码
         * @param code 状态码
         * @param msg 返回信息
         * @return
         */
        public static JsonData buildError(Integer code, String msg) {
            return new JsonData(code, null, msg);
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public Object getData() {
            return data;
        }
    
        public void setData(Object data) {
            this.data = data;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
  • 相关阅读:
    Silverlight 5 系列学习之一
    WPF中数据绑定问题
    细说ASP.NET Forms身份认证 别人写的不过很透彻就转来了以后用时再看
    再学IHanlder 类----------------关于Asp.net与iis原理网上看博客收获写一个验证码用一般处理程序记的好长时间前就写过不过现在再看有点不一样的感觉
    Oracle常用查看表结构命令
    尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。(遇到了这个问题网上查了下保存下来)
    TxetBox PasswordChar 模式解除
    屏幕抖动一 下
    oracle 日期问题 网上找到自己查阅时方便
    day5-Dns
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13323378.html
Copyright © 2011-2022 走看看