Springboot优雅处理异常,全局异常处理

**

开始

**

异常接口
自定义基础接口类
首先定义一个基础的接口类,自定义的错误描述枚举类需实现该接口。

package com.etc.common;

public interface BaseErrorInfo {

        String getResCode();
        String getRestMsg();
}

自定义的异常信息
自定义枚举类
然后我们这里在自定义一个枚举类,并实现该接口。

package com.etc.common;


public enum AbnoNum implements BaseErrorInfo {
    /**
     *
     */
    SUCCESS("200", "成功"),
    USERNAME_IS_NULL("-1", "用户名或密码为空"),
    USERPASS_IS_ERROR("-3", "密码错误"),
    USERNAME_IS_ENIST("-4", "用户不存在"),
    NullPoin("-5","空指针异常"),
    //秒杀订单异常
    MIAOSHAGOODS_IS_ENIST("300","已售罄"),
    ORDER_REPEAT("301","订单已存在"),
    order_error("302","订单创建失败"),
    //地址添加
    ADDRESS_ERROR("402","地址添加异常"),
    //密码修改错误
    PASSWORD_ERROR("503","密码修改失败"),
    OLDPASS_ERROR("504","原密码错误"),
    TWOPASS_ERROR("505","两次密码输入不一致"),
    NEWPASS_ERROR("506","新密码和原密码一样"),
    REQUEST("506","请求非法"),
    //秒杀模块
    ACCESS_LIMIT_FREQUENTLY("506","请求频繁"),

    ;


    private String resuleCode;
    private String resuleMsg;

    AbnoNum(String resuleCode, String resuleMsg) {
        this.resuleCode = resuleCode;
        this.resuleMsg = resuleMsg;
    }

    @Override
    public String getResCode() {
        return resuleCode;
    }

    @Override
    public String getRestMsg() {
        return resuleMsg;
    }
}

自定义异常类
然后我们在来自定义一个异常类,用于处理我们发生的业务异常。

package com.etc.common;

/**
 * @Author kalista
 * @Description
 * @Date 2020/7/15  10:08
 **/
public class ResultBean<T> {
    private String code;
    private String message;
    private Object data;

    private ResultBean() {

    }

    public static ResultBean error(BaseErrorInfo baseErrorInfo) {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(baseErrorInfo.getResCode());
        resultBean.setMessage(baseErrorInfo.getRestMsg());
        return resultBean;
    }

    public static ResultBean success() {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(AbnoNum.SUCCESS.getResCode());
        resultBean.setMessage(AbnoNum.SUCCESS.getRestMsg());
        return resultBean;
    }
    public static ResultBean success(Object data) {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(AbnoNum.SUCCESS.getResCode());
        resultBean.setMessage(AbnoNum.SUCCESS.getRestMsg());
        resultBean.setData(data);
        return resultBean;
    }



    public static ResultBean error(String errorcode, String errormsg) {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(errorcode);
        resultBean.setMessage(errormsg);
        return resultBean;
    }

    public static ResultBean error(String message) {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(AbnoNum.NullPoin.getResCode());
        resultBean.setMessage(message);
        return resultBean;
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

异常实体类
自定义数据格式
顺便这里我们定义一下数据的传输格式。

package com.etc.exitpation;

import lombok.Data;

/**
 * 处理业务异常
 */
@Data
public class BizExiption extends RuntimeException {
    /**
     * 错误码
     */
    protected String errorcode;
    /**
     * 错误信息
     */
    protected String errormsg;

    public BizExiption() {
       super();

    }

    public BizExiption(String resCode, String restMsg) {
        this.errorcode =resCode;
        this.errormsg = restMsg;
    }
}

异常处理类
自定义全局异常处理类
最后我们在来编写一个自定义全局异常处理的类。

package com.etc.exitpation;

import com.etc.common.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 勇于处理发生的业务异常
 */
@Slf4j
@ControllerAdvice
public class MyexitHeader {

    @ResponseBody
    @ExceptionHandler(value = com.etc.exitpation.BizExiption.class)
    public ModelAndView biz(com.etc.exitpation.BizExiption e) {
        log.error("发生了业务异常{}", e.getMessage());
        ModelAndView modelAndView = new ModelAndView("login");
        modelAndView.addObject("msg", e.getErrormsg());
        return modelAndView;
    }

    /**
     * 处理空
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = NullPointerException.class)
    public ModelAndView Nullposin(com.etc.exitpation.BizExiption e) {
        log.error("发生了空调指针异常{}", e.getMessage());
        ModelAndView modelAndView = new ModelAndView("login");
        modelAndView.addObject("msg", e.getErrormsg());
        return modelAndView;
    }

    /**
     * 处理其他异常
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResultBean Nullposin(Exception e) {
        e.printStackTrace();
        log.error("发生了其他异常{}", e.getMessage());
        return ResultBean.error(e.getMessage());
    }
}

测试 制造一个异常 就会被处理

throw new BizExiption(AbnoNum.USERNAME_IS_NULL.getResCode(), AbnoNum.USERNAME_IS_NULL.getRestMsg());
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值