NotReadablePropertyException:无效的属性&#39; <collection element =“”>&#39;验证字符串

时间:2017-08-29 14:24:08

标签: java spring hibernate validation

我正在使用spring boot 1.5.4.RELEASE

验证请求的内容
@RequestMapping(method = PATCH, value = "/{uniqueId}", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<UserResource> setGlobalRoles(@PathVariable("uniqueId") String uniqueId,
        @Valid @RequestBody UserPatch patch) { ... }

该课程如下:

public class UserPatch {
  @Valid
  private List<@GlobalRoleExists String> globalRoles = new ArrayList<>();

  ...

  public List<String> getGlobalRoles() {
    return globalRoles;
  }

  public void setGlobalRoles(List<String> globalRoles) {
    this.globalRoles = globalRoles;
  }

如果列表中的某个元素对约束无效,则会引发以下错误。

org.springframework.beans.NotReadablePropertyException: Invalid property 'globalRoles[1].<collection element>' of bean class [com.app.UserPatch]: Bean property 'globalRoles[1].<collection element>' 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) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:624) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.validation.AbstractBindingResult.getRawFieldValue(AbstractBindingResult.java:283) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.getRejectedValue(SpringValidatorAdapter.java:268) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:148) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:117) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.validation.DataBinder.validate(DataBinder.java:891) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.validateIfApplicable(AbstractMessageConverterMethodArgumentResolver.java:270) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:133) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]

我注意到路径“globalRoles [1]。&#39;由hibernate(org.hibernate.validator.internal.engine.path.NodeImpl)生成,但我不知道我是否遗漏了某些东西,或者是否有可能无法做到这一点。

1 个答案:

答案 0 :(得分:0)

从Spring Boot 1.4.x升级到1.5.6时遇到了同样的问题。

问题记录在Spring issue tracker,是由于在验证失败时Hibernate 5.3.x中生成的字符串发生了变化。

Spring Framework 4.3.11应该包含该修复,同时您可以明确指定对Hibernate版本5.2.5的依赖(如果您正在使用Gradle,则为compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.2.5.Final' 。)