gpt4 book ai didi

java - 我可以添加带有组的 bean 验证注释吗?

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:58 24 4
gpt4 key购买 nike

我想建立一个NotNull检查仅适用于 Create组,所以我可以这样写

@Data
public static class TestDto {

@NotNullOnCreate
// @NotNull(groups = Create.class) // instead of this
private String id;
}

我创建了 NotNullOnCreate像这样

@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
@Repeatable(NotNullOnCreate.List.class)
@ReportAsSingleViolation
@Constraint(validatedBy = {})
@NotNull(groups = Create.class)
@interface NotNullOnCreate {

String message() default "{javax.validation.constraints.NotNull.message}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
@interface List {

NotNullOnCreate[] value();
}
}

这不起作用,因为编写约束注释的组(请参阅 ConstraintDescriptorImpl#createComposingConstraintDescriptor )将设置为托管注释的组(“默认”)。

我也尝试过这个Class<?>[] groups() default {Create.class}; ,但是这是不允许的,默认组必须为空。

我怎样才能实现这个目标?我不想到处写组。

编辑

最佳答案

目前这是不可能的。如果您愿意,可以在 Hibernate Validator issue tracker 中提出问题我们可以探索这个功能。

关于java - 我可以添加带有组的 bean 验证注释吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44366294/

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