Python 官方文档:入门教程 => 点击学习
目录一、封装统一返回结果类二、自定义异常封装类三、错误枚举四、全局异常处理类五、测试一、封装统一返回结果类 import com.jiusen.exercise.enums.Er
import com.jiusen.exercise.enums.ErrorEnum;
import com.jiusen.exercise.exception.BusinessException;
import lombok.Getter;
import lombok.Setter;
@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;
@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;
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@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;
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@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);
}
}
到此这篇关于Spring Boot全局统一异常处理器的文章就介绍到这了,更多相关Spring Boot异常处理器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Spring Boot全局统一异常处理器
本文链接: https://www.lsjlt.com/news/125866.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0