No @NotBlank validator for type String

Myroslav picture Myroslav · Jan 19, 2018 · Viewed 16.5k times · Source

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.

Answer

Thanthla picture Thanthla · Oct 9, 2018

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>