温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Springboot中处理异常

发布时间:2021-06-03 15:50:14 来源:亿速云 阅读:195 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关怎么在Springboot中处理异常,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

一、制造异常

怎么在Springboot中处理异常

报500错误。在大量的代码中很难找到错误

怎么在Springboot中处理异常

二、统一异常处理

添加异常处理方法

GlobalExceptionHandler.java中添加

//指定出现什么异常执行这个方法     @ExceptionHandler(Exception.class)     @ResponseBody //为了返回数据     public R error(Exception e) {         e.printStackTrace();         return R.error().message("执行了全局异常处理..");     }

报错异常:在大型项目中,可对多种异常进行处理,便于找bug

怎么在Springboot中处理异常

三、特殊异常处理

定义异常,特别处理ArithmeticException异常

怎么在Springboot中处理异常

怎么在Springboot中处理异常

//特定异常处理     @ExceptionHandler(ArithmeticException.class)     @ResponseBody//为了返回数据     public R error(ArithmeticException e){         e.printStackTrace();         return R.error().message("执行了ArithmeticException异常处理");     }

异常处理结果:

怎么在Springboot中处理异常

四、自定义异常处理

第一步:创建自定义处理类的实体类:

@Data @AllArgsConstructor//生成有参构造方法 @NoArgsConstructor//生成无参构造方法 public class MyException extends RuntimeException{     private Integer code;     private String msg; }

第二步:在统一异常类中添加规则:

//自定义异常处理     @ExceptionHandler(MyException.class)     @ResponseBody//返回数据     public R error(MyException e){         e.printStackTrace();         return R.error().code(e.getCode()).message(e.getMsg());//封装自定义异常信息     }

第三步:执行自定义异常

try{            int i=10/0;        }catch (Exception e){            throw new MyException(20001,"执行自定义异常处理");        }

怎么在Springboot中处理异常

以上使用的R类,用于封装json数据的格式:

@Data public class R {     @ApiModelProperty(value = "是否成功")     private Boolean success;     @ApiModelProperty(value = "返回码")     private Integer code;     @ApiModelProperty(value = "返回消息")     private String message;     @ApiModelProperty(value = "返回数据")     private Map<String, Object> data = new HashMap<String, Object>();     private R(){}     public static R ok(){         R r = new R();         r.setSuccess(true);         r.setCode(ResultCode.SUCCESS);         r.setMessage("成功");         return r;     }     public static R error(){         R r = new R();         r.setSuccess(false);         r.setCode(ResultCode.ERROR);         r.setMessage("失败");         return r;     }     public R success(Boolean success){         this.setSuccess(success);         return this;     }     public R message(String message){         this.setMessage(message);         return this;     }     public R code(Integer code){         this.setCode(code);         return this;     }     public R data(String key, Object value){         this.data.put(key, value);         return this;     }     public R data(Map<String, Object> map){         this.setData(map);         return this;     } }
public interface ResultCode {     public static Integer SUCCESS = 20000;     public static Integer ERROR = 20001; }

关于怎么在Springboot中处理异常就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI