What does this nested annotation do / allow?

后端 未结 1 555
日久生厌
日久生厌 2021-02-05 22:51

I was looking at the @org.hibernate.validator.constaints.NotEmpty annotation:

@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, AN         


        
相关标签:
1条回答
  • 2021-02-05 23:05

    Reason why NotEmpty.List exists, is to go around the fact that same annotation cannot be repeated for same element. With the help of NotEmpty.List multiple NotEmpty annotations are effectively applied to one element. Annotation processing checks through the list of NotEmpty annotations that are the value of NotEmpty.List.

    In the case of NotEmpty one reason for using List of validators could be use of groups and assigning different messages per group.

    For the sake of example, let's take entity which can represent either company or person. In both cases name should not be null, but messages differ:

    @NotEmpty.List({
        @NotEmpty( message = "Person name should not be empty",   
                   groups=PersonValidations.class),
        @NotEmpty( message = "Company name should not be empty",    
                   groups=CompanyValidations.class),
    })
    private String name;
    
    0 讨论(0)
提交回复
热议问题