springboot异常集中处理
一,信息异常封装类
public class QYException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String msg;
private int code = 500;
public QYException(String msg) {
super(msg);
this.msg = msg;
}
public QYException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}
public QYException(String msg, int code) {
super(msg);
this.msg = msg;
this.code = code;
}
public QYException(String msg, int code, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
...get...set...
}
二,错误码和错误信息定义类
public enum QYExceptionCode {
SUCCESS(200),
WARN(301),
ERROR(500),
UNKNOW_EXCEPTION(10000, "系统未知异常,请联系管理员。"),
VAILD_EXCEPTION(10001, "参数格式校验失败"),
ORDER_EXCEPTION(10012, "订单系统异常");
private int code;
private String msg;
QYExceptionCode() {
}
QYExceptionCode(int code) {
this.code = code;
}
QYExceptionCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
...get...
}
三 全局异常处理器
统一封装 R
import com.kong.qyk8.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestControllerAdvice
public class ExceptionControllerAdvice {
@ExceptionHandler(QYException.class)
public R handleRRException(QYException e) {
log.error("处理自定义异常:code={},msg={}", e.getCode(), e.getMessage());
return new R().put("code", e.getCode()).put("msg", e.getMessage());
}
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public R handleVaildException(MethodArgumentNotValidException e) {
log.error("数据校验出现问题{},异常类型:{}", e.getMessage(), e.getClass());
BindingResult bindingResult = e.getBindingResult();
Map<String, String> errorMap = new HashMap<>();
bindingResult.getFieldErrors().forEach((fieldError) -> {
errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
});
return R.error(QYExceptionCode.VAILD_EXCEPTION.getCode(), QYExceptionCode.VAILD_EXCEPTION.getMsg()).put("data", errorMap);
}
@ExceptionHandler(NoHandlerFoundException.class)
public R handlerNoFoundException(NoHandlerFoundException e) {
log.error("路径不存在,请检查路径是否正确.{}", e.getMessage());
return R.error(404, "路径不存在,请检查路径是否正确");
}
@ExceptionHandler(DuplicateKeyException.class)
public R handleDuplicateKeyException(DuplicateKeyException e) {
log.error("数据库中已存在该记录.{}", e.getMessage());
return R.error("数据库中已存在该记录");
}
@ExceptionHandler(AuthorizationException.class)
public R handleAuthorizationException(AuthorizationException e) {
log.error("没有权限,请联系管理员授权{}", e.getMessage());
return R.error("没有权限,请联系管理员授权");
}
@ExceptionHandler(DemoModeException.class)
public R demoModeException(DemoModeException e) {
return R.error("演示模式,不允许操作");
}
@ExceptionHandler(Exception.class)
public R handleException(Exception e) {
log.error("未知异常,请联系管理员.{}{}", e.getMessage(),e);
return R.error(QYExceptionCode.UNKNOW_EXCEPTION.getCode(), QYExceptionCode.UNKNOW_EXCEPTION.getMsg());
}
}