错误:找不到类型验证器:java.time.LocalDate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30249829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: No validator could be found for type: java.time.LocalDate
提问by Humberto Corrêa
I'm working on a project that uses bean validation (Hibernate Validator 5.1.3.Final). My bean has a attribute with the @Past
annotation.
我正在开发一个使用 bean 验证的项目(Hibernate Validator 5.1.3.Final)。我的 bean 有一个带有@Past
注释的属性。
@Past(message = "A data deve estar no passado.")
private LocalDate dataAbertura;
But, when the validation occurs I get the following exception:
但是,当验证发生时,我收到以下异常:
21:46:12,424 ERROR [io.undertow.request] (default task-35) UT005023: Exception handling request to /financeiro/clientes/pessoafisica: javax.servlet.ServletException: javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.time.LocalDate.
at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:127) [vraptor-4.1.4.jar:]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access##代码##0(ServletInitialHandler.java:76) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(ServletInitialHandler.java:166) [undertow-servlet-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:197) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:759) [undertow-core-1.1.0.Final.jar:1.1.0.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_25]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_25]
Caused by: javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.time.LocalDate.
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.verifyResolveWasUnique(ConstraintValidatorManager.java:218) [hibernate-validator-5.1.3.Final.jar:5.1.3.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.findMatchingValidatorClass(ConstraintValidatorManager.java:193) [hibernate-validator-5.1.3.Final.jar:5.1.3.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.getInitializedValidator(ConstraintValidatorManager.java:97) [hibernate-validator-5.1.3.Final.jar:5.1.3.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:125) [hibernate-validator-5.1.3.Final.jar:5.1.3.Final]
What's wrong? Hibernate Validator doesn't support LocalDate?
怎么了?Hibernate Validator 不支持 LocalDate?
回答by Hardy
Actually the issue to refer to is HV-874. Hibernate Validator 5.2.x does add support for some of the new Java 8 date/time types. I should stress "some" in this context. In particular LocalDate
is not supported. The Javadocs of LocalDate
says:
实际上要参考的问题是HV-874。Hibernate Validator 5.2.x 确实添加了对一些新的 Java 8 日期/时间类型的支持。在这种情况下,我应该强调“一些”。特别LocalDate
是不支持。的 JavadocsLocalDate
说:
This class does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.
此类不存储或表示时间或时区。相反,它是对日期的描述,用于生日。如果没有偏移量或时区等附加信息,它就无法表示时间线上的瞬间。
Without an instant on the timeline it is not possible to say if a given date is in the part or future. First by attaching a timezone it would be possible to state and answer this question.
如果时间线上没有一个瞬间,就无法确定给定的日期是在零件中还是在未来。首先通过附加时区,可以说明和回答这个问题。
If you still think it makes sense in your use case to use the Past
and @Future
constraints for LocalDate
you can always implement your own ConstraintValidator
for LocalDate
and register it via XML using a constraint-definition
element in a constraint mapping file as seen here.
如果你仍然认为它使您的使用情况下,使用感Past
和@Future
约束LocalDate
你总是可以实现自己ConstraintValidator
的LocalDate
,并使用通过XML注册它constraint-definition
在约束映射文件元素看到这里。
If you are using Hibernate Validator 5.2, you can also use the Java ServiceLoader approach to register additional ConstraintValidator
implementations - see ConstraintDefinitionContributor. The latter is for now a Hibernate Validator specific feature.
如果您使用的是 Hibernate Validator 5.2,您还可以使用 Java ServiceLoader 方法来注册其他ConstraintValidator
实现 - 请参阅ConstraintDefinitionContributor。后者目前是 Hibernate Validator 特定的功能。
回答by EricF
Since LocalDate is still pretty new a lot of libraries don't handle it yet. In the project I am working on I had to write custom user type for LocalDate.
由于 LocalDate 仍然很新,很多图书馆还没有处理它。在我正在处理的项目中,我必须为 LocalDate 编写自定义用户类型。
Take a look at this hibernate issue HHH-8844
看看这个休眠问题HHH-8844
It seems like they have added a new module hibernate-java8 to support java 8's new Date API.
似乎他们添加了一个新模块 hibernate-java8 来支持 java 8 的新 Date API。
I know the above information is talking about type mapping but the same might hold true to validation.
我知道上述信息是在谈论类型映射,但同样适用于验证。
If there is no out of the box solution you might just have to write your own. See the following page for an example of adding joda time validators, java 8 dates should be similar link
如果没有现成的解决方案,您可能只需要编写自己的解决方案。有关添加 joda 时间验证器的示例,请参阅以下页面,java 8 日期应该是类似的链接
回答by Ruslan Sheremet
There is a @Past
annotation in the javax.validation.constraints
(validation-api-2.0.1.Final)
(validation-api-2.0.1.Final)中有一个@Past
注解javax.validation.constraints
And it works pretty well for java.time.LocalDate
它适用于 java.time.LocalDate