我正在尝试使用javax.validation验证某些DTO,但似乎@NotEmpty注释未检查参数是否为空。
这是我的专长:
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 + "]";
}
}
当我尝试通过以下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,然后再次添加它们,错误消失了。
答案 0 :(得分:1)
@NotEmpty在内部检查最小大小为1和是否为null。在@NotEmpty的文档中,明确提到了:
"Asserts that the annotated string, collection, map or array is not {@code null} or empty."
答案 1 :(得分:1)
我用您的代码片段创建了一个演示项目,它可以运行,您能否检查是否有任何差异?您是否真的导入了javax.validation.constraints.NotEmpty
。
您还可以共享一个演示实际问题的演示项目吗?您是否可以在测试中打印toString
对象的person
并共享
------------------------------------------------------
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
答案 2 :(得分:1)
@NotEmpty还检查@NotNull,并另外检查提供的对象的大小/长度。 对于上述测试用例,您将收到1次违规,因为您尚未设置作业名称(其空值),并且您配置了 @NotEmpty(message =“ mandatoryParametersMissing”) 私有字符串名称;
所以@NotEmpty涵盖了@NotNull情况。
我运行了您上面的代码,效果很好。我使用了以下依赖项
我运行了您的代码,它对我来说很好。 检查导入的@NotEmpty
org.hibernate.validator.constraints.NotEmpty
我使用了以下依赖项
<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>
答案 3 :(得分:0)
似乎问题出在依赖项上,由于某种原因,在类路径中,validation-api的jar版本是错误的,我只是删除了类路径中的所有jar,然后再次添加它们,错误消失了。