javax.validation.NotBlank missing validator

2020-07-09 10:16发布

问题:

I have requirement that in common api module(multi module project) I can't use any kind of hibernate's validation annotations, so I did use one from javax.validation which is acceptable.

Problem starts when I want to validate my domain objects(I use vaadin) that contains NotBlank annotation. I get the following exception

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'. Check configuration for 'name'

Validation is invoked by call

Validation.buildDefaultValidatorFactory().validateValue(beanType, propertyName, value)

Same code works perfectly with hibernate's NotBlank

Also @Size @NotNull from javax works fine.

Is it possible to provide NotBlank validator implementation to DefaultValidatorFactory?

Am I missing some dependency? (I have hibernate-validator already)

Does NotBlank from javax works the same as NotBlank from hibernate(I mean does it validate strings?)

How to solve this?

回答1:

The problem is in the version you are using then. You need to update to 6.0.x series. With the current latest been 6.0.9. Note that the groupId is changed to org.hibernate.validator.

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.9.Final</version>
</dependency>

the javax.validation.constraints.NotBlankis part of Bean Validation 2.0 and validator for it is not present in 5.3 series.



回答2:

as baeldung.com say Per the JSR 380 specification, the validation-api dependency contains the standard validation APIs:

<dependency>
  <groupId>javax.validation</groupId>
   <artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>

Hibernate Validator is the reference implementation of the validation API. To use it, we must add the following dependencies:

<dependency>
   <groupId>org.hibernate.validator</groupId>
   <artifactId>hibernate-validator</artifactId>
<version>6.0.2.Final</version>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.0.2.Final</version></dependency>