Myroslav
Myroslav

Reputation: 91

No @NotBlank validator for type String

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.

Upvotes: 9

Views: 18381

Answers (6)

Haroldo_OK
Haroldo_OK

Reputation: 7230

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>

Upvotes: 0

Benny64
Benny64

Reputation: 195

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.

Upvotes: 3

Thanthla
Thanthla

Reputation: 586

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>

Upvotes: 8

skcodes
skcodes

Reputation: 41

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>

Upvotes: -3

Varvarigos Emmanouil
Varvarigos Emmanouil

Reputation: 757

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>

Upvotes: 5

Yogi
Yogi

Reputation: 1895

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

  1. org.hibernate.validator.constraints.NotBlank
  2. javax.validation.constraints.NotNull

Upvotes: -1

Related Questions