Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'

I'm using Hibernate validator to validate my beans.

Actually I have this dependencies on my POM

    <!-- Hibernate validator - Bean validation API Implementation -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.11.Final</version>
    </dependency>

    <!-- Verify validation annotations usage at compile time -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>6.0.11.Final</version>
    </dependency>

    <!-- Unified Expression Language - Spec -->
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.1-b06</version>
    </dependency>

    <!-- Unified Expression Language - Implementation -->
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>2.2.6</version>
    </dependency>

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>

And this a pice of my bean

import org.hibernate.annotations.ColumnDefault;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;

public class Casa {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;

    @Column(name="nomcaac") @NotBlank(message = "{casa.nomcaac.null}") @Size(min = 0, max = 300, message = "{casa.nomcaac.size}")
    private String nomcaac;

Now the problem comes when i try to validate my bean, i get this error message

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

I can't figure out whats my problem, I think I have the right dependencies according to this page.

like image 358
Fede Jinich Avatar asked Oct 18 '18 16:10

Fede Jinich


People also ask

Can we use @NotBlank for long?

@NotEmpty: a constrained CharSequence, Collection, Map, or Array is valid as long as it's not null, and its size/length is greater than zero. @NotBlank: a constrained String is valid as long as it's not null, and the trimmed length is greater than zero.

What is Hibernate Validator?

Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.


1 Answers

@NotBlank is new in Bean Validation 2.0 so I suspect you have an old Hibernate Validator version (e.g. 5.x) in your classpath somehow.

You should check your dependency tree with mvn dependency:tree (and your webapp looking for an old jar).

By the way, your javax.el dependencies are incorrect: please refer to https://github.com/hibernate/hibernate-validator/#using-hibernate-validator for the correct one.

like image 145
Guillaume Smet Avatar answered Sep 23 '22 03:09

Guillaume Smet