springboot中的异常捕获与处理

服务端对外暴露的接口不可避免的会出现异常,无论是系统异常还是业务异常,出现异常后如何处理对用户提供友好的提示信息是我们所必须处理的。以下提供借助spring注解@ControllerAdvice实现对控制层、业务层异常的统一处理。

(1)在前后端分离的开发模式中,后端不再作视图跳转,仅用作为前端返回数据,我们多使用@Controller + @ResponseBody或直接使用@RestController注解的方式实现将数据转换为json格式返回,通常我们都会定义一个通用的返回类用于对返回数据的封装:

@Data
public class BaseResult {

    /**
     * 状态码
     */
    private int code;

    /**
     * 返回数据
     */
    private Object data;

    /**
     * 返回信息
     */
    private String message;

    public BaseResult() {
    }

    public BaseResult(Object data) {
        this.data = data;
    }

    public BaseResult(int code, Object data) {
        this.code = code;
        this.data = data;
    }

    public BaseResult(int code, Object data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public BaseResult success(){
        this.code = HttpStatus.OK.value();
        this.data = true;
        this.message = "请求成功";
        return this;
    }

    public BaseResult success(Object data){
        this.code = HttpStatus.OK.value();
        this.data = data;
        this.message = "请求成功";
        return this;
    }

    public BaseResult error(){
        this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
        this.data = false;
        this.message = "请求失败";
        return this;
    }

    public BaseResult error(Object data,String message){
        this.code = 500;
        this.data = data;
        this.message = message;
        return this;
    }
}

(2) 自定义业务异常类:

public class CustomUserException extends RuntimeException {

    private Integer code;

    public CustomUserException(UserResponseEnum userResponseEnum){
        super(userResponseEnum.getDescription());
        this.code = userResponseEnum.getCode();
    }

    public Integer getCode() {
        return code;
    }
}
---------------------------
在构造自定义业务异常对象时使用了 枚举 的方式,将常见的业务错误提示语对应的错误代码进行
映射,枚举类如下所示:

public enum UserResponseEnum {

    USER_NOT_FOUND(50001,"用户不存在"),

    USER_AUTHENTICATION_ERROR(50002,"用户密码不正确");

    private Integer code;

    private String description;

    UserResponseEnum(Integer code, String description) {
        this.code = code;
        this.description = description;
    }

    public Integer getCode() {
        return code;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

(3)通过@ControllerAdvice注解,实现统一异常捕获:

@RestController
@ControllerAdvice(basePackages = {"com.blade.system.controller"})
public class CustomExceptionAdvice {

    private static final Logger logger = LoggerFactory.getLogger(CustomExceptionAdvice.class);

    /**
     * 处理与用户相关的业务异常
     * @return
     */
    @ExceptionHandler(CustomUserException.class)
    public BaseResult UserExceptionHandler(HttpServletRequest request,CustomUserException e){
        logger.error("用户信息异常:Host:{} invoke URL:{},错误信息:{}",request.getRemoteHost(),request.getRequestURL(),e.getMessage());
        return new BaseResult(e.getCode(),false,e.getMessage());
    }
}

(4)在业务代码中在需要抛出异常的地方抛出对应的异常即可:

	/**
     * 根据主键获取用户实体
     * @param id
     * @return
     */
    public User selectByKey(String id) {
        User user = userMapper.selectByPrimaryKey(id);
        if (user == null) {
            throw new CustomUserException(UserResponseEnum.USER_NOT_FOUND);
        }
        return user;
    }
    --------------------------------------
    返回信息如下:
    {
	    "code": 50001,
	    "data": false,
	    "message": "用户不存在"
	}
	前端即可根据返回信息对用户进行友好的提示。
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值