2
votes

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.

1
Are the getter and setters are public? - mallikarjun
The error message doesn't seem to have anything to do with your code. Post real code that reproduces the issue, with the real and complete exception stack trace. - JB Nizet
i use lombok @Data and in *.class files i saw getters and setters - Igor Fedorov
I would say the problem is not validation but incorrect path in your HTML. Looks that you use properties[1] somewhere in your path. Please post the JSP you use - StanislavL
I use jackson objectMapper to get object from json. After deserialize i get normal java object and some fields has null value. Field with null value annotated with @NotNull (codeName). I call SmartValidator.validate and then i get exception. I validate full valid java object and SmartValidator.validate must create FieldError from codeName field, but it throw exception - Igor Fedorov

1 Answers

1
votes

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