No @NotBlank validator for type String
Asked Answered
F

6

9

I keep getting validator error for quite simple configuration class

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;

@Component
@Data
@Validated
@ConfigurationProperties(prefix = "test")
public class TestProperties {
    @NotBlank
    String uri;
}

Error is self explaining:

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

But according to spring boot documentation this should work.

Firstrate answered 19/1, 2018 at 11:56 Comment(2)
It turns out Spring Boot uses Hibernate validator to make things work. I tries to use Bean Validation 2 annotations with Hibernate 5. Which obviously didn't work.Firstrate
you're a life saver! spring-boot-starter-web 1.5.8 was using hibernate-validator 5.x.x. So I just added an exclusion to that dependency and fixed my issue. You should make an answer and mark it!Celebrant
B
8

I had the same issue and solved it.

As Yogi stated in his answer, there are two implementations:

import javax.validation.constraints.NotBlank;

and

import org.hibernate.validator.constraints.NotBlank;

Switching from the javax implementation to the hibernate one solved the issue.

Since I didn't use Spring boot, I had to add to my pom.xml:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.2.4.Final</version>
</dependency>
Burgomaster answered 9/10, 2018 at 7:56 Comment(1)
import org.hibernate.validator.constraints.NotBlank; is depricatedSpringfield
C
5

According to Spring-Boot documentation and maven central repo the:

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

has the validator included. If you have not spring-boot-starter-web included to your dependencies you can just add the validator:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Cochineal answered 19/1, 2018 at 12:29 Comment(0)
C
3

I saw a similar question outside this forum and discovered that the error comes up whenever you try to put @NotBlank annotation on any non-string type field. In my case, the javax.validation.UnexpectedTypeException came up when I annotated a field of type Byte with @NotBlank. So I imported javax.validation.constraints.NotNull, changed the annotation on the Byte field to @NotNull and the error disappeared.

I previously tried replacing import javax.validation.constraints.NotBlank; with import org.hibernate.validator.constraints.NotBlank; to kill the error, but I got a deprecation warning. Programmers are discouraged from using deprecated program elements, so I advise that you use @NotNull annotation instead.

To understand the differences between @NotBlank and @NotNull, see this StackOverflow thread. That way, you get to decide if @NotNull fits your use case.

Coccid answered 17/10, 2018 at 12:41 Comment(0)
A
0

In my case, it worked okay when running as a standalone application, but gave this same error when running as a WAR; in order to make it work, it was necessary to explicitly import the hibernate-validator as a dependency in order to make it work when running as a WAR:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>7.0.2.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator-annotation-processor</artifactId>
</dependency>
Arcograph answered 2/3, 2022 at 20:0 Comment(0)
N
-1

You can use below options,which works with Spring JPA:

  1. org.hibernate.validator.constraints.NotBlank
  2. javax.validation.constraints.NotNull
Nickeliferous answered 19/1, 2018 at 12:11 Comment(0)
W
-3

I have always used @NotNull.

I think the corresponding POM code is

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

Wrecker answered 19/1, 2018 at 13:47 Comment(1)
That's not what he was asking about. @NotNull and @NotBlank have different semantics for StringsUnreality

© 2022 - 2024 — McMap. All rights reserved.