[英]Annotation @NotEmpty doesn't check if String is null
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.我正在尝试使用 javax.validation 验证一些 DTO,但似乎注释 @NotEmpty 没有检查参数是否为空。
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:当我尝试通过以下 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);
}
And here's the method that validates the DTOs:这是验证 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());
}
}
I was under the impression that @NotEmpty first checked if the parameter was null, ¿am I wrong?我的印象是@NotEmpty 首先检查参数是否为空,¿我错了吗?
PD: Here are the dependencies I use: 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>
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.似乎问题出在依赖项上,由于某种原因,在类路径中,validation-api 的 jar 版本是错误的,我只是删除了类路径中的所有 jar 并再次添加它们,错误消失了。
@NotEmpty 在内部检查最小大小为 1 和 null。在@NotEmpty 的文档中,明确提到:
"Asserts that the annotated string, collection, map or array is not {@code null} or empty."
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
.你真的导入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您能否在测试中打印person
对象的toString
并共享您的输出
------------------------------------------------------
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
@NotEmpty checks for @NotNull also and additionally checks the size/length of the provided object. @NotEmpty 还会检查 @NotNull 并另外检查所提供对象的大小/长度。 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;对于您的上述测试用例,您将收到 1 次违规,因为您尚未设置作业名称(其为空)并且您的配置显示 @NotEmpty(message = "mandatoryParametersMissing") private String name;
So @NotEmpty covers your @NotNull case.所以@NotEmpty 涵盖了你的 @NotNull 案例。
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检查 @NotEmpty 的导入
org.hibernate.validator.constraints.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>
似乎问题出在依赖项上,由于某种原因,在类路径中,validation-api 的 jar 版本是错误的,我只是删除了类路径中的所有 jar 并再次添加它们,错误消失了。
Check the import for @NotEmpty :检查 @NotEmpty 的导入:
org.hibernate.validator.constraints.NotEmpty
It works for me!这个对我有用!
I use Spring Boot 2.5.5
and Hibernate validator 6.2.0.final
.我使用 Spring Boot 2.5.5
和 Hibernate 验证器6.2.0.final
。
Notice that javax.validation.constraints.NotBlank
not work!请注意javax.validation.constraints.NotBlank
不起作用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.