2012-10-25 57 views
0

應該我有這個類ConstraintValidatorContext商店時,它不

@HasNose 
@HasMouth 
public class Person { 

    String fieldA; 
    String fieldB; 
    // getters and setters 

} 

現在定製註釋驗證這個樣子

// validator of @HasNose 
@Override 
public boolean isValid(Object object, 
         ConstraintValidatorContext context) { 

    // some tests that determine there is an error 
    context.disableDefaultConstraintViolation(); 
      context.buildConstraintViolationWithTemplate("Some message") 
      .addNode("fieldA") 
      .addConstraintViolation(); 

} 

// validator of @HasMouth 
@Override 
public boolean isValid(Object object, 
         ConstraintValidatorContext context) { 

    // some tests that determine there is an error 
    context.disableDefaultConstraintViolation(); 
      context.buildConstraintViolationWithTemplate("Some message") 
      .addNode("fieldB") 
      .addConstraintViolation(); 

} 

運行此產生前面的節點例外:

java.lang.IllegalStateException: JSR-303 validated property 'fieldA.fieldB' 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:148) 
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:101) 
at org.springframework.validation.DataBinder.validate(DataBinder.java:722) 
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.validateIfApplicable(ModelAttributeMethodProcessor.java:154) 
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:107) 
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:74) 
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:155) 
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117) 
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) 
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900) 
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827) 
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
at java.lang.Thread.run(Thread.java:680) 

出於某種原因「ConstraintValidatorContext」將兩個字段連接在一起。調試顯示屬性「propertyPath」已經用「fieldA」填充。 我認爲它應該在每個約束驗證程序的根級別上嗎?

回答

0

我發現我自己的問題的解決方案,我使用的是hibernate-validator-4.2.0.BETA2。切換到hibernate-validator-4.2.0.FINAL解決了這個問題。

教訓:讓你的庫更新...

+0

你甚至應該升級到4.3.0.Final。這個版本的幾個性能改進。關閉這個問題也是一個好主意。 – Hardy