Springboot自定义全局异常

自定义全局异常

bizException基础包下封装异常类

BizException:运行时业务中出现的异常

/**
 * @Description : 运行时业务中出现的异常
 * @Param :
 * @Return :
 * @Author : l-jiahui
 * @Date : 2020-10-11
 */
public class BizException extends RuntimeException {

    private static final long serialVersionUID = -7864604160297181941L;

    private final String code;

    /**
     * @Description : 指定枚举类中的错误类
     * @Param : [errorCode]
     * @Return :
     * @Author : l-jiahui
     * @Date : 2020-10-11
     */
    public BizException(final BizExceptionCode exceptionCode) {
        super(exceptionCode.getMessage());
        this.code = exceptionCode.getCode();
    }
    /**
     * @Description : 指定具体业务错误的信息
     * @Param : [detailedMessage]
     * @Return :
     * @Author : l-jiahui
     * @Date : 2020-10-11
     */
    public BizException(final String message) {
        super(message);
        this.code = BizExceptionCodeEnum.SPECIFIED.getCode();
    }

    public String getCode() {
        return code;
    }
}

BizExceptionCode: 业务异常的错误代码接口

/**
 * @Description : 业务异常的错误代码接口
 * @Param :
 * @Return :
 * @Author : l-jiahui
 * @Date : 2020-10-11
 */

public interface BizExceptionCode {

    /**
     * @Description : 获取错误代码
     * @Param : []
     * @Return : java.lang.String
     * @Author : l-jiahui
     * @Date : 2020-10-11
     */
    String getCode();
    
    /**
     * @Description : 获取错误信息
     * @Param : []
     * @Return : java.lang.String
     * @Author : l-jiahui
     * @Date : 2020-10-11
     */
    String getMessage();

}

BizExceptionCodeEnum:异常消息的枚举类(此类属于业务异常枚举类)

/**
 * @Description : 异常消息的枚举类(此类属于业务异常枚举类)
 * @Param :
 * @Return :
 * @Author : l-jiahui
 * @Date : 2020-10-11
 */
public enum BizExceptionCodeEnum implements BizExceptionCode{

    // 已指明的异常,在异常使用时message并不返回前端,返回前端的为throw新的异常时指定的message
    SPECIFIED("-1","系统发生异常,请稍后重试"),

    // 常用业务异常
    USER_NAME_NULL("-1","用户名不能为空,请重新输入!"),
    USER_PASSWORD_NULL("-1","密码不能为空,请重新输入!"),
    USER_PASSWORD_WRONG("-1","密码错误,请检查后重新输入!"),
    PAGE_NUM_NULL("4001","页码不能为空"),
    PAGE_SIZE_NULL("4002","页数不能为空"),
    SEARCH_NULL("4004","搜索条件不能为空,请检查后重新输入!"),
    NO_LOGIN("3001", "用户未进行登录")
    ;

    private final String code;

    private final String message;

    /**
     * @Description :
     * @Param : [code, message]
     * @Return :
     * @Author : l-jiahui
     * @Date : 2020-10-11
     */
     BizExceptionCodeEnum(String code,String message){

        this.code = code;
        this.message = message;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }
}

全局捕获异常和自定义全局捕获异常以及404处理

/**
 * @author l-jiahui
 * extend of {@link ResponseEntityExceptionHandler} for handle all exception
 * @Description: 全局捕获异常和自定义全局捕获异常
 */
@ControllerAdvice
public class GlobalControllerAdvice {

    /**
     * 拦截捕捉自定义异常 BizException.class
     *
     * @param bizException 自定义异常
     * @return map
     */
    @ResponseBody
    @ExceptionHandler(value = BizException.class)
    public Map<String, Object> myExceptionHandler(BizException bizException, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("code", bizException.getCode());
        map.put("msg", bizException.getMessage());
        response.setStatus(Integer.parseInt(bizException.getCode()));
        return map;
    }

    /**
     * 增加处理404的统一错误信息
     *
     * @param response response对象
     * @return 返回map对应的对象信息
     */
    @ExceptionHandler(value = {NoHandlerFoundException.class})
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    public Map<String, Object> notFoundException(HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("code", "404");
        map.put("msg", "not found exception");
        response.setStatus(404);
        return map;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值