Spring 验证 JSR-303 @NotNull 抛出 NotReadablePropertyException
Spring validation JSR-303 @NotNull throw NotReadablePropertyException
我有两个 classes
@Data
@NoArgsConstructor
public class SearchProductOffer {
@Valid
private List<MatchProperty> properties = new ArrayList<>();
}
@Data
@NoArgsConstructor
public class MatchProperty {
private String version;
private String sourceSystem;
@NotNull(message = "Версия должна быть заполнена!")
public String codeName;
}
当我用 ValidationUtil.invokeValidator 验证 A 时,我得到
2018-07-06 16:04:09.769 ERROR 10223 --- [nio-8080-exec-1]
o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for
servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is
java.lang.IllegalStateException: JSR-303 validated property
'properties[1].codeName' does not have a corresponding accessor for
Spring data binding - check your DataBinder's configuration (bean
property versus direct field access)] with root cause
org.springframework.beans.NotReadablePropertyException: Invalid
property 'properties[1]' of bean class [java.lang.String]: Bean
property 'properties[1]' is not readable or has an invalid getter
method: Does the return type of the getter match the parameter type of
the setter? at
org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622)
~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
当 fieldB 为空时。两个 class 都有 getter/setter。
Spring 启动 2.0.3.RELEASE
更新
我每次都会遇到异常,而我想在内部 class.
中使用 @NotNull(字段值为空)或 @NotEmpty(字段为“”)字段进行验证
解决方案:删除 spring SmartValidator。
在控制器中配置活页夹
@InitBinder
private void initBinder(WebDataBinder dataBinder) {
dataBinder.initDirectFieldAccess();
}
在启动配置中配置验证器
@Bean
public LocalValidatorFactoryBean getValidator() {
return new LocalValidatorFactoryBean();
}
并将其注入控制器
@Autowired
private LocalValidatorFactoryBean validator;
之后我们可以调用
List<ConstraintViolation<Object>> validationResult = new ArrayList<>();
validationResult.addAll(validator.validate(request));
validationResult.addAll(validator.validate(methodBean));
并检查 validationResult 是否存在验证错误
我有两个 classes
@Data
@NoArgsConstructor
public class SearchProductOffer {
@Valid
private List<MatchProperty> properties = new ArrayList<>();
}
@Data
@NoArgsConstructor
public class MatchProperty {
private String version;
private String sourceSystem;
@NotNull(message = "Версия должна быть заполнена!")
public String codeName;
}
当我用 ValidationUtil.invokeValidator 验证 A 时,我得到
2018-07-06 16:04:09.769 ERROR 10223 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: JSR-303 validated property 'properties[1].codeName' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access)] with root cause
org.springframework.beans.NotReadablePropertyException: Invalid property 'properties[1]' of bean class [java.lang.String]: Bean property 'properties[1]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
当 fieldB 为空时。两个 class 都有 getter/setter。 Spring 启动 2.0.3.RELEASE
更新 我每次都会遇到异常,而我想在内部 class.
中使用 @NotNull(字段值为空)或 @NotEmpty(字段为“”)字段进行验证解决方案:删除 spring SmartValidator。 在控制器中配置活页夹
@InitBinder
private void initBinder(WebDataBinder dataBinder) {
dataBinder.initDirectFieldAccess();
}
在启动配置中配置验证器
@Bean
public LocalValidatorFactoryBean getValidator() {
return new LocalValidatorFactoryBean();
}
并将其注入控制器
@Autowired
private LocalValidatorFactoryBean validator;
之后我们可以调用
List<ConstraintViolation<Object>> validationResult = new ArrayList<>();
validationResult.addAll(validator.validate(request));
validationResult.addAll(validator.validate(methodBean));
并检查 validationResult 是否存在验证错误