A N
A N

Reputation: 53

Spring Boot validation exception : org.springframework.beans.NotReadablePropertyException: for [java.util.ArrayList]

I am trying to validate a list of objects being passed to my rest api. For validating the list param I have written a custom validator which iterates over the list and calls the SpringValidatorAdapter.validate instance.

Rest Param : List(Member) members.

The members in turn has a list of objects as an instance variable. e.g List booksIssued

Spring boot validation fails with below exception while validating the internalObject even though I have all the public getters and setters:

org.springframework.beans.NotReadablePropertyException: Invalid property 'booksIssued[0]' of bean class [java.util.ArrayList]: Bean property 'booksIssued' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

Bean Class :

@Data
public class Member implements Serializable{
    @NotNull
    private String id;
    @Size(min=2, max=30)
    private String name;
    @JsonProperty("books")
    @Valid
    private List<Book> booksIssued;

@Data
public static class Book implements Serializable {
    @NotNull
    @JsonProperty("booklabel")
    private String label;
    @Size(min = 4, max = 50)
    private String description;
  }
}

Rest Request :

@RequestMapping(value = "/members", method = RequestMethod.POST)
public ResponseEntity issueBooksToMember(@RequestBody List<Member> members){

Validator :

public void validate(List<Member> members) {

    for(Member object : members){
        ValidationUtils.invokeValidator(validator, object, errors);
    }
}

I am getting the above mentioned exception while validating the booksIssued for the Member.

Input json :

[
{ 
    "id" : "member1",
    "name" : "ab",
    "books" : [
     {
         "booklabel" : "label1",
         "description" : "des"
     }]
},
{
    "id" : "member2",
    "name" : "cd",
    "books" : [{
         "booklabel" : "label2",
         "description" : "desc2"
    }]
}
]

Stack trace :

Caused by: java.lang.IllegalStateException: JSR-303 validated property 'booksIssued[0].description' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access)
    at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:162)
    at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:102)
    at org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:83)
    at org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:55)
    ... 85 more
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'booksIssued[0]' of bean class [java.util.ArrayList]: Bean property 'booksIssued[0]' 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:633)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getNestedPropertyAccessor(AbstractNestablePropertyAccessor.java:850)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyAccessorForPropertyPath(AbstractNestablePropertyAccessor.java:827)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622)
    at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
    at org.springframework.validation.AbstractBindingResult.getRawFieldValue(AbstractBindingResult.java:283)
    at org.springframework.validation.beanvalidation.SpringValidatorAdapter.getRejectedValue(SpringValidatorAdapter.java:268)
    at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:148)

Upvotes: 2

Views: 730

Answers (1)

vbg
vbg

Reputation: 778

Seems like org.springframework.data.rest.core.ValidationErrors has a bug in accessing nested List or array. It can't access the element of the List by index.

After fighting with this issue for days I suddenly found a solution: replace List with Set! Try it if it fits your logic as Set can't have duplicate values. For me it fixed the issue.

Upvotes: 0

Related Questions