gpt4 book ai didi

java - 具有多个值的 Spring Boot 自定义约束 validator

转载 作者:行者123 更新时间:2023-12-03 07:54:29 24 4
gpt4 key购买 nike

我想为我的自定义约束 validator 添加 2 个值,因为我有 2 个功能标志:

 @JsonProperty(value = "name")
@BlockedWithoutEnabledFeatureFlag(feature = FeatureFlag.AAA, values = {"aaa", "bbb"})
@BlockedWithoutEnabledFeatureFlag(feature = FeatureFlag.BBB, values = {"ccc", "ddd"})
private String parameter;

地点:

@Constraint(validatedBy = {BlockedWithoutEnabledFeatureFlagValidator.class})
@Target({FIELD, PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@ReportAsSingleViolation
public @interface BlockedWithoutEnabledFeatureFlag {

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

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

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

FeatureFlag feature();

String[] values() default {};
}

和实现:

public class BlockedWithoutEnabledFeatureFlagValidator implements ConstraintValidator<BlockedWithoutEnabledFeatureFlag, Object> {
private final FeatureFlagService featureFlagService;

private List<String> blocked;
private FeatureFlag feature;

@Override
public void initialize(BlockedWithoutEnabledFeatureFlag constraintAnnotation) {
blocked = Arrays.asList(constraintAnnotation.values());
feature = constraintAnnotation.feature();
}

@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {

但现在我收到编译错误“重复注释”。如何做到这一点?

最佳答案

您应该使您的 BlockedWithoutEnabledFeatureFlag 可重复。为此:

  1. 创建新的“顶级”注释:
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatableBlockedWithoutEnabledFeatureFlag {
BlockedWithoutEnabledFeatureFlag[] value();
}
  • 将 @Repeatable 添加到您的 BlockedWithoutEnabledFeatureFlag 注释
  • ...
    @Repeatable(RepeatableBlockedWithoutEnabledFeatureFlag.class)
    public @interface BlockedWithoutEnabledFeatureFlag {
    ...

    那么您应该能够添加多个 @BlockedWithoutEnabledFeatureFlag 注释。

    关于java - 具有多个值的 Spring Boot 自定义约束 validator ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76390978/

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