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.
–
–
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>
–
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.