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

我正在尝试使用 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 测试时,我失败了:


@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,我错了吗?


慕的地8271018
浏览 187回答 5
5回答

三国纷争

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

慕沐林林

检查 @NotEmpty 的导入:org.hibernate.validator.constraints.NotEmpty这个对我有用!我使用 Spring Boot2.5.5和 Hibernate 验证器6.2.0.final。请注意,这javax.validation.constraints.NotBlank不起作用!

繁星点点滴滴

我用你的代码片段创建了一个演示项目并且它可以工作,你能检查一下有什么不同吗?你真的导入了吗javax.validation.constraints.NotEmpty?------------------------------------------------------ T E S T S-------------------------------------------------------Running nl.martinvw.test.PersonTestjul 17, 2019 2:38:01 PM org.hibernate.validator.internal.util.Version <clinit>INFO: HV000001: Hibernate Validator 6.1.0.Alpha5Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.353 secResults :Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

喵喔喔

还会检查 @NotNull 并另外检查所提供对象的大小/长度。对于您上面的测试用例,您将获得 1 次违规,因为您没有设置作业名称(其为空)并且您配置为 @NotEmpty(message = "mandatoryParametersMissing") private String name;所以 @NotEmpty 涵盖了您的 @NotNull 案例。我运行了您上面的代码并且运行良好。我使用了以下依赖项我运行了您的代码,它对我来说工作正常。检查@NotEmpty 的导入org.hibernate.validator.constraints.NotEmpty我使用了以下依赖项&nbsp; &nbsp;<dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.hibernate</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>hibernate-validator</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>6.0.13.Final</version>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.glassfish</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>javax.el</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>3.0.0</version>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <!-- https://mvnrepository.com/artifact/junit/junit -->&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>junit</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>4.12</version>&nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; </dependency>

德玛西亚99

似乎问题出在依赖项上,由于某种原因在类路径中 validation-api 的 jar 版本是错误的我只是删除了类路径中的所有 jar 并再次添加它们并且错误消失了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java