I have two 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;
}
while i validate A with ValidationUtil.invokeValidator i get
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]
when fieldB is null. Both classes have getter/setter. Spring Boot 2.0.3.RELEASE
UPDATE I get exception every time, while i want validate with @NotNull(while field value is null) or @NotEmpty(while my field is "") field in inner class.
Solution: remove spring SmartValidator. Configure binder in controller
@InitBinder
private void initBinder(WebDataBinder dataBinder) {
dataBinder.initDirectFieldAccess();
}
Configure validator in boot config
@Bean
public LocalValidatorFactoryBean getValidator() {
return new LocalValidatorFactoryBean();
}
And inject it into controller
@Autowired
private LocalValidatorFactoryBean validator;
after it we can call
List<ConstraintViolation<Object>> validationResult = new ArrayList<>();
validationResult.addAll(validator.validate(request));
validationResult.addAll(validator.validate(methodBean));
and check validationResult for validation errors
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With