daredesm
daredesm

Reputation: 615

javax.validation.NotBlank missing validator

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?

Upvotes: 10

Views: 23791

Answers (4)

Murat Yıldız
Murat Yıldız

Reputation: 12050

Add the following dependency to your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

If the changes don't take effect, then restart IntelliJ IDEA.

Upvotes: 0

Yaro
Yaro

Reputation: 684

Validation-api 2.0.1 contains javax.validation.constraints.NotBlank which is replacement for Hibernate's NotBlank. To use it you need dependency:

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

then in code

@NotBlank 
private String possibleBlankString;

Upvotes: 1

billschen
billschen

Reputation: 789

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>

Upvotes: 0

mark_o
mark_o

Reputation: 2533

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.

Upvotes: 14

Related Questions