52

I want to underline that I already searched for this kind of problem but couldn't find a solution for my case. In my Spring Boot webapp I keep getting this error when validating beans using @NotEmpty or @NotBlank annotation of package javax.validation.constraints:

14:04:59,426 ERROR [org.springframework.boot.web.servlet.support.ErrorPageFilter] (default task-33) Forwarding to error page from request [/registrati
on] due to exception [HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.String'.
 Check configuration for 'username']: javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation
.constraints.NotEmpty' validating type 'java.lang.String'. Check configuration for 'username'
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:229)
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:310)
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(Constraint
Tree.java:244)

No errors if I use @NotNull annotation, but this is not the desired behavior because it allows for blank fields. These are my pom.xml dependencies:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
          <groupId>nz.net.ultraq.thymeleaf</groupId>
          <artifactId>thymeleaf-layout-dialect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-batch</artifactId>
       </dependency>
       <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.21</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

        <!-- webjars -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.1.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.webjars</groupId>
                    <artifactId>jquery</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.webjars/datatables -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>datatables</artifactId>
            <version>1.10.19</version>
            <exclusions>
                <exclusion>
                    <groupId>org.webjars</groupId>
                    <artifactId>jquery</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <!-- plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin -->
        </plugins>
    </build>


</project>

I see that hibernate validator is working because if I don't use any @NotEmpty nor @NotBlank annotation, other annotations such as @Size are working correctly.

In my bean I'm importing javax.validation.constraints. When starting up my JBoss, following line about hibernate validator appears:

14:04:17,676 INFO  [org.hibernate.validator.internal.util.Version] (background-preinit) HV000001: Hibernate Validator 5.3.5.Final-redhat-2

This is not the same version as the hibernate-validator 6.0.11 jar that is resolved by Maven.

What's happening? Maybe some dependency conflict?

Thanks to everyone that could help me.

Simon
  • 596
  • 1
  • 4
  • 12
  • I'm deploying my application to Wildfly 10.1 which implements Java EE7 spec. I'm using spring-boot-starter-parent version 2.1.1 with spring-boot-start-web. This last one pulls in bean validation api 2.0 which is a Java EE8 specification. I'm wondering if that may be the reason. I'm still messing with my environment. See this: https://stackoverflow.com/questions/50751653/jboss-eap-7-1-spring-boot-application-no-validator-could-be-found-for-constra – Chuck L Jan 04 '19 at 17:09

10 Answers10

53

I got a similiar error but ...Constraints.NotEmpty' validating type 'java.lang.Long'

It happened because I was using @NotEmpty in a non-String field.

I solved it replacing @NotEmpty with @Size(value=1, message= "whatever")

dmarquina
  • 3,728
  • 1
  • 28
  • 28
  • 4
    According the err. message, the author is using @NotEmpty on a String. – JRA_TLL Jan 27 '21 at 09:07
  • I am getting the similar error for validating type 'java.time.OffsetDateTime'. Code (method argument): `@NotNull @Pattern(regexp = "^(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3})Z$") @ApiParam(value = "ISO DateTime Format yyyy-MM-dd'T'HH:mm:ss.SSS'Z', e.g. '2000-10-31T01:30:00.000Z'.", required = true) @Valid @RequestParam(value = "fromDate", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) OffsetDateTime fromDate` Can we validate regex for type OffsetDateTime? – Prem Sep 15 '22 at 14:52
21
  1. @NotBlank : Checks that the annotated character sequence is not null and the trimmed length is greater than 0.
  2. @NotNull : Checks that the annotated value is not null, however it can be empty.
  3. @NotEmpty : Checks whether the annotated element is not null nor empty.

Before use the above annotation, have to think about how it works.

for further reading use this article : Read more article one Read more article two

homaxto
  • 5,428
  • 8
  • 37
  • 53
LalithK90
  • 960
  • 7
  • 15
10

I have the same problem; my spring-boot-starter-web dependency includes a dependency on org.hibernate:hibernate-validator:5.3.6.Final but I. I added this exclusion to my POM and it's all all good now:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-validator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>
  • 1
    Unfortunately in my case, excluding hibernate-validator dependency from spring-boot-starter-web causes compilation error in my bean class because import javax.validation.constraints.* are not resolved. If, in addition to the step above, I add validation-api then I get the error "no Bean Validation provider could be found". The only way I can get it to work is to keep Hibernate validator dependency inside spring-boot-starter-web and in my User bean class import org.hibernate.validator.constraints.* even if they're deprecated. I really cannot understand what am I doing wrong. Please help... – Simon Oct 05 '18 at 13:10
  • 2
    If you exclude the _hibernate-validator_ dependency you need to use `org.hibernate.validator.constraints.NotEmpty` instead of `javax.validation.constraints.NotEmpty` – Benjamin Mullard Oct 08 '18 at 10:12
  • @Simon I have the exact same issue as you. Were you able to solve it or are you sticking with the deprecated imports? – Chuck L Jan 04 '19 at 15:58
  • @ChuckL unfortunately I had not time to investigate further on this issue. I'm still using deprecated imports. If you find a solution, please, update this post. I'll do the same in case I'll have time. – Simon Jan 10 '19 at 16:48
  • thanks for the answer! In my case, the dependency tree showed that the hibernate-validator was a transitive dependency from a lot of my other dependencies. After excluding from all of them, using the javax.validation.constraints.NotEmpty worked as expected. – Bhavi Daftery Jagwani Jun 29 '20 at 18:41
  • Instead of exclusion for older version of `hibernate-validator`, I use newer version directly in Pom : ``` org.hibernate hibernate-validator 6.1.6.Final ``` – Ridox Sep 06 '21 at 07:45
4

Thanks to ChuckL comment I just found out that JBoss 7.1 has its own hibernate-validator-5.3.5.Final-redhat-2.jar under:

modules\system\layers\base\org\hibernate\validator\main

That's why I was seeing version 5.3.5.Final-redhat-2 in my startup logs and not the one packaged in my WAR. It also has validation-api-1.1.0.Final-redhat-1.jar under:

modules\system\layers\base\javax\validation\api\main

One could try to exclude those dependencies by creating a jboss-deployment-structure.xml file in the root META-INF of the webapp and trying to use those packaged in the application WAR/JAR.

Anyway JBoss 7.1 is a JEE7 implementation container. Changing the bean validation modules that are shipped with JBoss could lead to unexpected consequences that I don't want to experiment :)

The clean solution would be to migrate JBoss to a newer version which supports JEE8, otherwise keep using deprecated APIs.

Simon
  • 596
  • 1
  • 4
  • 12
  • Thanks for the update @Simon! Yeah I've been meaning to submit an answer but you covered it all. I installed Wildfly 15, deployed my application, and everything worked with no errors. :-) So like you said, either continue using deprecated APIs or move up in server version that implements JEE8 spec. – Chuck L Jan 16 '19 at 19:27
3

adding newer version in pom.xml fixed this error:

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

I encountered this error, because I used another version of validator api directly. Like this:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
        <scope>compile</scope>
    </dependency>
Ridox
  • 1,073
  • 13
  • 18
  • I was facing same problem with version 5.4.3, updating to 6.1.6 fixed the problem. I am wondering what was the actual reason behind this. I used those annotation on string, still the error was UnexpectedTypeException, which is missleading – mahfuj asif Nov 20 '22 at 04:02
1

Your hibernate validator is implementing the Java Bean Validation 1.1 specification, but @NotBlank is from Java Bean Validation 2.0. As a result, you have three options:

  1. Upgrade your hibernate validator so that it implements the Java Bean Validation 2.0 specification
  2. Remove @NotBlank, since your version of hibernate validator does not support it.
  3. Remove hibernate validator and lose bean validation features, but this error will go away.
joseph
  • 2,429
  • 1
  • 22
  • 43
0

Change the @NotEmpty import from org.hibernate.validator.constraints.NotEmpty;

-1

@NotEmpty should only be used for String type attributes.

For mandatory data you should use @NotNull.

And it also has @Future and @Past annotations to validate whether it is a future or past date.

-2

This error means you are trying to Validate an attribute of some other type than the one the annotation is made for

e.g. I got below error while trying to validate an Enum with a NotBlank annotation

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'com.example.irrigation.dtos.TimeSlot'. Check configuration for 'timeSlot'

so, @NotBlank, @NotEmpty is for validating String They cannot be used for an enum.

If try to mix data Type (e.g. Enum for @NotBlank, etc.) then UnexpectedTypeException will be thrown.

Akash Verma
  • 638
  • 1
  • 11
  • 15
-3

Just go with @NotNull and will work

  • In the question: `No errors if I use @NotNull annotation, but this is not the desired behavior because it allows for blank fields.` – Eric Aya Feb 06 '22 at 13:17