Spring Boot验证异常:org.springframework.beans.NotReadablePropertyException:for [java.util.ArrayList]

时间:2017-08-03 09:43:29

标签: rest validation spring-boot

我正在尝试验证传递给我的rest api的对象列表。为了验证列表参数,我编写了一个自定义验证器,它遍历列表并调用SpringValidatorAdapter.validate实例。

Rest Param:列出(会员)成员。

成员又将对象列表作为实例变量。例如 列出书籍已发表

Spring验证失败,但在验证internalObject时出现以下异常,即使我拥有所有公共getter和setter:

org.springframework.beans.NotReadablePropertyException:无效的属性' booksIssued [0]' of bean class [java.util.ArrayList]:Bean属性' booksIssued'不可读或getter方法无效:getter的返回类型是否与setter的参数类型匹配?

Bean类:

@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;
  }
}

休息请求:

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

验证者:

public void validate(List<Member> members) {

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

我在验证为会员发布的图书时收到了上述异常。

输入json:

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

堆栈追踪:

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)

0 个答案:

没有答案