前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot自定义异常基本步骤模板

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

作者头像
用户9006224
发布2022-12-21 09:22:14
3840
发布2022-12-21 09:22:14
举报
文章被收录于专栏:cjz的专栏

自定义异常处理

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

代码语言:javascript
复制
public class MyException extends RuntimeException {

    private int status; //状态码

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

支持枚举代码:

代码语言:javascript
复制
/**
 * 自定义异常类
 */

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) 简单代码模板:

代码语言:javascript
复制
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 ;
    }

}

支持返回的结果集的:

代码语言:javascript
复制
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已经集成了

代码语言:javascript
复制
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
</dependency>
代码语言:javascript
复制
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");
    }
}

自定义异常枚举

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

代码语言:javascript
复制
@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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 自定义异常处理
  • 捕获异常
  • 自定义异常结果
  • 自定义异常枚举
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档