这篇文章主要介绍Spring Boot全局统一异常处理器的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
import com.jiusen.exercise.enums.ErrorEnum;
import com.jiusen.exercise.exception.BusinessException;
import lombok.Getter;
import lombok.Setter;
/**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 统一的返回结果
*/
@Getter
@Setter
public class AjaxResult {
//是否成功
private Boolean success;
//状态码
private Integer code;
//提示信息
private String msg;
//数据
private Object data;
public AjaxResult() {
}
//自定义返回结果的构造方法
public AjaxResult(Boolean success, Integer code, String msg, Object data) {
this.success = success;
this.code = code;
this.msg = msg;
this.data = data;
}
//自定义异常返回的结果
public static AjaxResult defineError(BusinessException de) {
AjaxResult result = new AjaxResult();
result.setSuccess(false);
result.setCode(de.getErrorCode());
result.setMsg(de.getErrorMsg());
result.setData(null);
return result;
}
//其他异常处理方法返回的结果
public static AjaxResult otherError(ErrorEnum errorEnum){
AjaxResult result = new AjaxResult();
result.setMsg(errorEnum.getErrorMsg());
result.setCode(errorEnum.getErrorCode());
result.setSuccess(false);
result.setData(null);
return result;
}
}
import lombok.Getter;
import lombok.Setter;
/**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 类描述
*/
@Getter
@Setter
public class BusinessException extends RuntimeException{
private static final long serialVersionUID = 1L;
/**
* 错误状态码
*/
protected Integer errorCode;
/**
* 错误提示
*/
protected String errorMsg;
public BusinessException() {
}
public BusinessException(String message, Integer errorCode, String errorMsg) {
super(message);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
}
**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 类描述
*/
public enum ErrorEnum {
//数据操作错误定义
SUCCESS(200, "成功"),
NO_PERMISSION(403, "你无权访问"),
NO_Auth(401, "未授权,请登录验证"),
NO_FOUND(404, "未找到资源"),
INTERNAL_SERVER_ERROR(500, "服务器异常, 请联系管理员!");
/**
* 错误码
*/
private Integer errorCode;
/**
* 错误信息
*/
private String errorMsg;
ErrorEnum(Integer errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public Integer getErrorCode() {
return errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
}
import com.jiusen.exercise.enums.ErrorEnum;
import com.jiusen.exercise.exception.BusinessException;
import com.jiusen.exercise.rest.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 类描述
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 处理自定义异常
* @param e
* @return
*/
@ExceptionHandler(value = BusinessException.class)
public AjaxResult bizExceptionHandler(BusinessException e) {
log.error(e.getErrorMsg(), e);
return AjaxResult.defineError(e);
}
/**
* 处理其它异常
*/
@ExceptionHandler(value = Exception.class)
public AjaxResult exceptionHandler(Exception e) {
log.error(e.getMessage(), e);
return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
}
}
import com.jiusen.exercise.enums.ErrorEnum;
import com.jiusen.exercise.exception.BusinessException;
import com.jiusen.exercise.rest.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 类描述
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 处理自定义异常
* @param e
* @return
*/
@ExceptionHandler(value = BusinessException.class)
public AjaxResult bizExceptionHandler(BusinessException e) {
log.error(e.getErrorMsg(), e);
return AjaxResult.defineError(e);
}
/**
* 处理其它异常
*/
@ExceptionHandler(value = Exception.class)
public AjaxResult exceptionHandler(Exception e) {
log.error(e.getMessage(), e);
return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
}
}
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
以上是“Spring Boot全局统一异常处理器的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。