SpringBoot自定义异常基本步骤模板

自定义异常处理

继承你要自定义异常的类,例如我要对RuntimeException自定义异常
简单代码模板:

public class MyException extends RuntimeException {

    private int status; //状态码

    public MyException( int status,String message) {
        super(message);
        this.status = status;
    }
}

支持枚举代码:


/**
 * 自定义异常类
 */

public class LyException extends RuntimeException {

    private int status; //状态码

    public LyException(String message,int status) {
        super(message);
        this.status = status;
    }

    /**
     * 支持枚举
     * @param ceshisglShog
     */
    public LyException(ExceptionEnums ceshisglShog) {
        super(ceshisglShog.getMessage());
        this.status = ceshisglShog.getStatus();
    }

    public int getStatus() {
        return status;
    }
}

捕获异常

写完自定义异常是不生效的,原因就是SpringBoot不知道,所以要捕获异常
在类上添加 @ControllerAdvice 在方法上添加 @ExceptionHandler(自定义异常类.class)
简单代码模板:

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;

@ControllerAdvice // 对controller中的方法做增强,做异常处理的增强
public class ControllerExceptionAdvice {

/*
*这个方法的返回类型,可以是一个结果类
*/
    @ExceptionHandler(MyException.class) //写自定义异常类或者你要拦截的异常类,如Exception异常类
    public String exceptionHandler(MyException ex){
    	//异常内容
        String message = ex.getMessage();
        
        /*
        * 具体内容根据需求来
        */
        
		// 返回异常结果
        return message ;
    }

}

支持返回的结果集的:

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;


/**
 * 捕获自定义异常
 */
@ControllerAdvice
public class BasicExceptionAdvice {

    @ExceptionHandler(LyException.class)//拦截的自定义异常类
    public ResponseEntity handleException(LyException ex){

        return ResponseEntity.status(ex.getStatus()).body(new ExceptionResult(ex));
    }
}

自定义异常结果

上面两步骤,根据不是特别好,可以来一个 自定义异常结果类
我这个结果类里用到了日期工具类:JodaTime
版本不用写SpringBoot已经集成了

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
</dependency>
import lombok.Getter;
import org.joda.time.DateTime;

/**
 * 自定义异常结果类
 */
@Getter //记得导入lombok,不用直接就来一个 Get方法
public class ExceptionResult {
    private int status;//状态码
    private String message; //内容
    private String timestamp;//时间

    public ExceptionResult(LyException e) {
        this.status = e.getStatus();
        this.message = e.getMessage();
        this.timestamp = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
    }
}

自定义异常枚举

最后可以来一个枚举类,里面放内容和状态码

@Getter //记得导入lombok,不用直接就来一个 Get方法
public enum  ExceptionEnums {
	//有多少定义多少
    ITEM_PRICE_NOT_NULL(501,"价格不能为空"),
    UPLOAD_FILE_ERROR(502,"上传失败,请重试");
    
    private int status;
    private String message;//内容
    
    ExceptionEnums(int status, String message) {
        this.status = status;
        this.message = message;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值