相关文章推荐
奔跑的沙发  ·  python ...·  1 年前    · 
有胆有识的小蝌蚪  ·  Android Material ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
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.

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. – Myroslav Jan 19, 2018 at 12:27 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! – jon.mervine Mar 28, 2018 at 23:00

I had the same issue and solved it.

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

import javax.validation.constraints.NotBlank;
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>
<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>

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.

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>

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

That's not what he was asking about. @NotNull and @NotBlank have different semantics for Strings – Alex Savitsky Jan 19, 2018 at 14:28

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.