参数级联校验失败时ValidationExceptionAdvice反射获取注解异常
Closed this issue · 5 comments
OPbjO commented
graceful版本:3.1.0
测试代码:
TestController
@RestController
@RequestMapping(value = "/test")
@Slf4j
public class TestController {
@PostMapping("/validated")
public void validated(@RequestBody @Validated JobInfoRequest jobInfoRequest) {
log.info("jobInfoRequest={}", JSON.toJSONString(jobInfoRequest));
}
}
校验参数对象,其中JobDetail对象进行级联校验
@Data
public class JobInfoRequest {
@NotBlank(message = "岗位名称不能为空")
@ValidationStatusCode(code = "501")
private String jobName;
@Valid
@NotNull(message = "详细信息不能为空")
@ValidationStatusCode(code = "501")
private JobDetail jobDetail;
}
@Data
public class JobDetail {
@NotNull(message = "薪水不能为空")
@Min(value = 3000, message = "不能低于300元")
@ValidationStatusCode(code = "501")
private BigDecimal pay;
@NotNull(message = "招聘人数不能为空")
@Min(value = 1, message = "至少招聘1人")
@ValidationStatusCode(code = "501")
private Integer headCount;
private Date updateTime;
}
post请求参数
{
"jobName": "程序员",
"jobDetail": {
"pay": 2000.50,
"headCount": 20,
"updateTime": "2023-09-30 20:34:08"
}
}
pay最小值为3000,校验失败,抛出MethodArgumentNotValidException,进入com.feiniaojin.gracefulresponse.advice.ValidationExceptionAdvice全局异常处理器。
在com.feiniaojin.gracefulresponse.advice.ValidationExceptionAdvice#fromBindException第118行,使用反射获取校验失败字段时,外部对象持有的内部对象字段报错,所以fieldName=jobDetail.pay,但是target.getClass()获得的是外部对象JobInfoRequest.class,因此无法获取到jobDetail.pay字段,抛出异常NoSuchFieldException。
feiniaojin commented
系统提示:您的邮件被腾讯邮箱判定为垃圾邮件,请编辑后重新发送!
feiniaojin commented
OPbjO commented
OPbjO commented
@feiniaojin
3.1.0代码:
3.2.0-boot2代码:
处理异常的顺序变了
feiniaojin commented
MethodArgumentNotValidException是BindException的子类