javax.validation.NotBlank missing validator
Asked Answered
E

4

10

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?

Eva answered 25/4, 2018 at 8:9 Comment(6)
can you tell which version of hibernate-validator do you have ?Ishmaelite
What type of object you are using on this annotation ? Integer ? String ?Disaffirm
Its 5.3.6.finalEva
Validated field looks like this @NotBlank @Size(min = 1, max = 250) private String name;Eva
Okay, are you using the spring boot starter-web ? if not are you including this dependency ? <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>Disaffirm
Yes, I use, that was the cause of the problem > some nested dependency that I wasn't aware of.Eva
I
14

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.

Ishmaelite answered 25/4, 2018 at 8:43 Comment(1)
In the parent module we have latest version of the hibernate-validator(properties). However in module I work with I also have vaadin dependency which have dependency to spring-boot-starter-web which have this older validator version. I thought I had the newest one. Glad that went quick! ThanksEva
A
1

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;
Airborne answered 16/8, 2021 at 14:12 Comment(0)
S
0

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>
Steeplechase answered 6/9, 2018 at 12:44 Comment(0)
M
0

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.

Macdonell answered 5/11, 2022 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.