spring boot 全局异常处理及自定义异常类
2018-03-02 16:30
816 查看
全局异常处理:
定义一个处理类,使用@ControllerAdvice注解。
@ControllerAdvice注解:控制器增强,一个被@Component注册的组件。
配合@ExceptionHandler来增强所有的@requestMapping方法。
例如:@ExceptionHandler(Exception.class) 用来捕获@requestMapping的方法中所有抛出的exception。
代码:@ControllerAdvice
public class GlobalDefultExceptionHandler {
//声明要捕获的异常
@ExceptionHandler(Exception.class)
@ResponseBody
public String defultExcepitonHandler(HttpServletRequest request,Exception e) {
return “error”;
}
}这样,全局异常处理类完毕。可以添加自己的逻辑。
然后还有一个问题,有的时候,我们需要业务逻辑时抛出自定义异常,这个时候需要自定义业务异常类。
定义class:BusinessException ,使他继承于RuntimeException.
说明:因为某些业务需要进行业务回滚。但spring的事务只针对RuntimeException的进行回滚操作。所以需要回滚就要继承RuntimeException。public class BusinessException extends RuntimeException{
}然后,现在来稍微完善一下这个类。
当我们抛出一个业务异常,一般需要错误码和错误信息。有助于我们来定位问题。
所以如下:public class BusinessException extends RuntimeException{
//自定义错误码
private Integer code;
//自定义构造器,只保留一个,让其必须输入错误码及内容
public BusinessException(int code,String msg) {
super(msg);
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}这时候,我们发现还有一个问题,如果这样写,在代码多起来以后,很难管理这些业务异常和错误码之间的匹配。所以在优化一下。
把错误码及错误信息,组装起来统一管理。
定义一个业务异常的枚举。public enum ResultEnum {
UNKONW_ERROR(-1,"未知错误"),
SUCCESS(0,"成功"),
ERROR(1,"失败"),
;
private Integer code;
private String msg;
ResultEnum(Integer code,String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}这个时候,业务异常类:public class BusinessException extends RuntimeException{
private static final long serialVersionUID = 1L;
private Integer code; //错误码
public BusinessException() {}
public BusinessException(ResultEnum resultEnum) {
super(resultEnum.getMsg());
this.code = resultEnum.getCode();
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}然后再修改一下全局异常处理类:@ControllerAdvice
public class GlobalDefultExceptionHandler {
//声明要捕获的异常
@ExceptionHandler(Exception.class)
@ResponseBody
public <T> Result<?> defultExcepitonHandler(HttpServletRequest request,Exception e) {
e.printStackTrace();
if(e instanceof BusinessException) {
Log.error(this.getClass(),"业务异常:"+e.getMessage());
BusinessException businessException = (BusinessException)e;
return ResultUtil.error(businessException.getCode(), businessException.getMessage());
}
//未知错误
return ResultUtil.error(-1, "系统异常:\\n"+e);
}
}判断这个是否是业务异常。和系统异常就可以分开处理了。
定义一个处理类,使用@ControllerAdvice注解。
@ControllerAdvice注解:控制器增强,一个被@Component注册的组件。
配合@ExceptionHandler来增强所有的@requestMapping方法。
例如:@ExceptionHandler(Exception.class) 用来捕获@requestMapping的方法中所有抛出的exception。
代码:@ControllerAdvice
public class GlobalDefultExceptionHandler {
//声明要捕获的异常
@ExceptionHandler(Exception.class)
@ResponseBody
public String defultExcepitonHandler(HttpServletRequest request,Exception e) {
return “error”;
}
}这样,全局异常处理类完毕。可以添加自己的逻辑。
然后还有一个问题,有的时候,我们需要业务逻辑时抛出自定义异常,这个时候需要自定义业务异常类。
定义class:BusinessException ,使他继承于RuntimeException.
说明:因为某些业务需要进行业务回滚。但spring的事务只针对RuntimeException的进行回滚操作。所以需要回滚就要继承RuntimeException。public class BusinessException extends RuntimeException{
}然后,现在来稍微完善一下这个类。
当我们抛出一个业务异常,一般需要错误码和错误信息。有助于我们来定位问题。
所以如下:public class BusinessException extends RuntimeException{
//自定义错误码
private Integer code;
//自定义构造器,只保留一个,让其必须输入错误码及内容
public BusinessException(int code,String msg) {
super(msg);
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}这时候,我们发现还有一个问题,如果这样写,在代码多起来以后,很难管理这些业务异常和错误码之间的匹配。所以在优化一下。
把错误码及错误信息,组装起来统一管理。
定义一个业务异常的枚举。public enum ResultEnum {
UNKONW_ERROR(-1,"未知错误"),
SUCCESS(0,"成功"),
ERROR(1,"失败"),
;
private Integer code;
private String msg;
ResultEnum(Integer code,String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}这个时候,业务异常类:public class BusinessException extends RuntimeException{
private static final long serialVersionUID = 1L;
private Integer code; //错误码
public BusinessException() {}
public BusinessException(ResultEnum resultEnum) {
super(resultEnum.getMsg());
this.code = resultEnum.getCode();
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}然后再修改一下全局异常处理类:@ControllerAdvice
public class GlobalDefultExceptionHandler {
//声明要捕获的异常
@ExceptionHandler(Exception.class)
@ResponseBody
public <T> Result<?> defultExcepitonHandler(HttpServletRequest request,Exception e) {
e.printStackTrace();
if(e instanceof BusinessException) {
Log.error(this.getClass(),"业务异常:"+e.getMessage());
BusinessException businessException = (BusinessException)e;
return ResultUtil.error(businessException.getCode(), businessException.getMessage());
}
//未知错误
return ResultUtil.error(-1, "系统异常:\\n"+e);
}
}判断这个是否是业务异常。和系统异常就可以分开处理了。
相关文章推荐
- Spring Boot - 全局异常处理
- SpringBoot----全局异常处理
- Spring boot 全局异常处理
- Spring-Boot(五) 全局异常处理
- spring boot 学习--03---web控制层全局异常处理
- spring boot全局异常处理
- spring boot学习教程(4):全局异常处理代码demo
- SpringBoot全局异常处理
- SpringBoot学习之全局异常处理设置(返回JSON)
- SpringBoot全局异常处理
- SpringBoot系列五:SpringBoot错误处理(数据验证、处理错误页、全局异常)
- springboot全局异常控制处理
- Spring Boot 全局异常处理
- springBoot注解大全JPA注解springMVC相关注解全局异常处理
- SpringBoot入门——局部与全局的异常处理
- 第四十五章 SpringBoot全局异常处理
- springboot全局异常处理
- springboot-20-全局异常处理
- spring boot之全局异常处理代码demo
- 轻松实现SpringBoot项目异常全局处理