gpt4 book ai didi

java - 注解@NotEmpty不检查String是否为null

转载 作者:行者123 更新时间:2023-11-30 05:33:32 26 4
gpt4 key购买 nike

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

这是我的类(class):

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

}

工作类别

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 测试时,我遇到了失败:

@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首先检查参数是否为空,我错了吗?

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 的文档中,明确提到:
“断言带注释的字符串、集合、映射或数组不是 {@code null} 或空。”

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com