5

I'm trying to validate some DTOs with javax.validation, but it seems that the annotation @NotEmpty doesn't check of the parameter is null.

Here are my clases:

Person.class

public class Person {

    @NotEmpty(message = "mandatoryParametersMissing")
    private String name;

    @NotNull(message = "mandatoryParametersMissing")
    @Valid
    private Job job;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Job getJob() {
        return job;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", job=" + job + "]";
    }

}

Job.class

public class Job {

    @NotEmpty(message = "mandatoryParametersMissing")
    private String name;

    @NotNull(message = "mandatoryParametersMissing")
    private Integer salary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSalary() {
        return salary;
    }

    public void setSalary(Integer salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Job [name=" + name + ", salary=" + salary + "]";
    }

}

When I try to pass the following JUnit testS I get failures:

@Test(expected = BusinessServiceException.class)
    public void testJobNameNull() {
        Person samuel = new Person();
        samuel.setName("Samuel Antequera");
        Job programmer = new Job();

        programmer.setSalary(18000);
        samuel.setJob(programmer);

        validatePerson(samuel);
    }

And here's the method that validates the DTOs:

public void validatePerson(Person in) {
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();

    Set<ConstraintViolation<Person>> violations = validator.validate(in);
    for (ConstraintViolation<Person> violation : violations) {
        throw new BusinessServiceException(violation.getMessage(), violation.getPropertyPath().toString());
    }
}

I was under the impression that @NotEmpty first checked if the parameter was null, ¿am I wrong?

PD: Here are the dependencies I use:

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

    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.1.0.Alpha5</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>6.1.0.Alpha5</version>
    </dependency>

    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.1-b06</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>2.2.6</version>
    </dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

It seems the problem was with the dependencies, for some reason in the class path the jar version of validation-api was wrong I simply deleted all the jars in the class path and added them again and the error disappears.

  • Setting the fields to an empty value and not null does trigger the validation error? – Giacomo Alzetta Jul 17 '19 at 12:16
  • Nope, it still doesn't trigger the validation error. I'm starting to think that the problem is with the version of my dependencies, I added them to the post. –  Jul 17 '19 at 12:22
  • Uhm. It looks like the validator is not recognizing the annotations. This may be an issue with the dependencies. You should put a break point before the call to `validate` and try to debug step-by-step... hopefully at some point you will find where the validator obtains the fields and corresponding annotations and you should be able to check why the validator ignores them. Anyway: I'd start from using a stable release instead of an alpha version... – Giacomo Alzetta Jul 17 '19 at 12:23
  • 1
    Thanks for the help, it seems the problem was with the dependencies, for some reason in the class path the jar version of validation-api was wrong. –  Jul 22 '19 at 09:20
  • I'm glad you have resolved your issue. You can answer your own question clarifying how you solved it and accept your own answer. – Giacomo Alzetta Jul 22 '19 at 10:14

5 Answers5

2

@NotEmpty internally checks for minimum size of 1 and null.On documentation of @NotEmpty, it is clearly mentioned that :
"Asserts that the annotated string, collection, map or array is not {@code null} or empty."

1

Check the import for @NotEmpty :

org.hibernate.validator.constraints.NotEmpty

It works for me!

I use Spring Boot 2.5.5 and Hibernate validator 6.2.0.final.

Notice that javax.validation.constraints.NotBlank not work!

Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42
Leon Deng
  • 29
  • 4
0

I created a demo-project with your code fragments and it works, could you maybe check for any differences? Did you really import javax.validation.constraints.NotEmpty.

Could you also share a demo-project which shows the actual problem? Could you maybe print the toString of the person object in your test and share the output of your

See demo-project

------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running nl.martinvw.test.PersonTest
jul 17, 2019 2:38:01 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 6.1.0.Alpha5
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.353 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
  • Thanks for the help, it seems the problem was with the dependencies, for some reason in the class path the jar version of validation-api was wrong. –  Jul 22 '19 at 09:20
0

@NotEmpty checks for @NotNull also and additionally checks the size/length of the provided object. For your above test case you will be getting 1 violation because you have not set the job name (its null) and you config says @NotEmpty(message = "mandatoryParametersMissing") private String name;

So @NotEmpty covers your @NotNull case.

I ran your above code and it worked fine. I used the below dependency

I ran your code and its working fine for me. Check the import for @NotEmpty

org.hibernate.validator.constraints.NotEmpty

I used the below dependency

   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.13.Final</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
ashit07
  • 39
  • 1
  • 5
0

It seems the problem was with the dependencies, for some reason in the class path the jar version of validation-api was wrong I simply deleted all the jars in the class path and added them again and the error disappears.