4

I have the following code

import javax.validation.constraints.NotBlank;
...
@NotBlank(message="Not Blank")
private String thing;

When I try to run I get

No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'

How do I use NotBlank annotation with a string?

Java 8

Jackie
  • 21,969
  • 32
  • 147
  • 289

2 Answers2

3

The javax.validation.constraints.NotBlank annotation you are applying is part of Bean Validation API v2.

The most probable case is:

  • You have bean validation API v2 as dependency on your project
  • The implementing framework/library supports only v1.0/v1.1 API.

Please check on this post for information about existing implementations: Is there JSR-303 implementation available?

I recommend you checking for bean validation API v2 on your classpath (it also can be called JSR-380). The only implementation of this API I know about is Hibernate Validator v6.0. Make sure that v2 implementation is not hidden by some v1.x implementation being on classpath.

Aleh Maksimovich
  • 2,622
  • 8
  • 19
  • Sure I will try to add some of the dependencies I think might matter. What libraries would have API v1 as a dependency? Spring, etc. – Jackie Dec 04 '17 at 04:27
0

I have encountered this problem and solved it. Because the validation version conflicts, In my project, the error was introduced twice validate。check the jar package, and then remove, then successful。

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.1.0.Final</version>
</dependency>
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
YaSuo
  • 1
  • 2