我正在尝试使用javax.validation验证某些DTO,但似乎@NotEmpty注释未检查参数是否为null。

这是我的案例:

人类

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 + "]";
    }

}


工作班

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 + "]";
    }

}


当我尝试通过以下JUnit testS时,我会失败:

@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);
    }


这是验证DTO的方法:

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());
    }
}


我的印象是@NotEmpty首先检查参数是否为null,这是错的吗?

PD:这是我使用的依赖项:

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


似乎问题出在依赖项上,由于某种原因,在类路径中,validation-api的jar版本是错误的,我只是删除了类路径中的所有jar,然后再次添加它们,错误消失了。

最佳答案

@NotEmpty内部检查最小大小为1和null。@ NotEmpty的文档中明确提到:
"Asserts that the annotated string, collection, map or array is not {@code null} or empty."

关于java - 注释@NotEmpty不检查String是否为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57075273/

10-08 22:59