feat:GlobalExceptionHandler 支持更多请求参数不对的问题
This commit is contained in:
@@ -6,6 +6,8 @@ import cn.hutool.core.map.MapUtil;
|
|||||||
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.ObjUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.extra.servlet.JakartaServletUtil;
|
import cn.hutool.extra.servlet.JakartaServletUtil;
|
||||||
|
import cn.iocoder.yudao.framework.common.biz.infra.logger.ApiErrorLogCommonApi;
|
||||||
|
import cn.iocoder.yudao.framework.common.biz.infra.logger.dto.ApiErrorLogCreateReqDTO;
|
||||||
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
||||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||||
@@ -14,8 +16,6 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|||||||
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
||||||
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
||||||
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
||||||
import cn.iocoder.yudao.framework.common.biz.infra.logger.ApiErrorLogCommonApi;
|
|
||||||
import cn.iocoder.yudao.framework.common.biz.infra.logger.dto.ApiErrorLogCreateReqDTO;
|
|
||||||
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.validation.ConstraintViolation;
|
import jakarta.validation.ConstraintViolation;
|
||||||
@@ -29,6 +29,7 @@ import org.springframework.util.Assert;
|
|||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
import org.springframework.validation.ObjectError;
|
import org.springframework.validation.ObjectError;
|
||||||
|
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||||
@@ -101,6 +102,9 @@ public class GlobalExceptionHandler {
|
|||||||
if (ex instanceof HttpRequestMethodNotSupportedException) {
|
if (ex instanceof HttpRequestMethodNotSupportedException) {
|
||||||
return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex);
|
return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex);
|
||||||
}
|
}
|
||||||
|
if (ex instanceof HttpMediaTypeNotSupportedException) {
|
||||||
|
return httpMediaTypeNotSupportedExceptionHandler((HttpMediaTypeNotSupportedException) ex);
|
||||||
|
}
|
||||||
if (ex instanceof ServiceException) {
|
if (ex instanceof ServiceException) {
|
||||||
return serviceExceptionHandler((ServiceException) ex);
|
return serviceExceptionHandler((ServiceException) ex);
|
||||||
}
|
}
|
||||||
@@ -179,9 +183,11 @@ public class GlobalExceptionHandler {
|
|||||||
if (ex.getCause() instanceof InvalidFormatException) {
|
if (ex.getCause() instanceof InvalidFormatException) {
|
||||||
InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause();
|
InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause();
|
||||||
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue()));
|
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue()));
|
||||||
} else {
|
|
||||||
return defaultExceptionHandler(ServletUtils.getRequest(), ex);
|
|
||||||
}
|
}
|
||||||
|
if (StrUtil.startWith(ex.getMessage(), "Required request body is missing")) {
|
||||||
|
return CommonResult.error(BAD_REQUEST.getCode(), "请求参数类型错误: request body 缺失");
|
||||||
|
}
|
||||||
|
return defaultExceptionHandler(ServletUtils.getRequest(), ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -237,6 +243,17 @@ public class GlobalExceptionHandler {
|
|||||||
return CommonResult.error(METHOD_NOT_ALLOWED.getCode(), String.format("请求方法不正确:%s", ex.getMessage()));
|
return CommonResult.error(METHOD_NOT_ALLOWED.getCode(), String.format("请求方法不正确:%s", ex.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理 SpringMVC 请求的 Content-Type 不正确
|
||||||
|
*
|
||||||
|
* 例如说,A 接口的 Content-Type 为 application/json,结果请求的 Content-Type 为 application/octet-stream,导致不匹配
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
|
||||||
|
public CommonResult<?> httpMediaTypeNotSupportedExceptionHandler(HttpMediaTypeNotSupportedException ex) {
|
||||||
|
log.warn("[httpMediaTypeNotSupportedExceptionHandler]", ex);
|
||||||
|
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求类型不正确:%s", ex.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理 Spring Security 权限不足的异常
|
* 处理 Spring Security 权限不足的异常
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user