当前位置: 首页 > 面试题库 >

具有多个字段的Spring自定义批注验证

章昊
2023-03-14
问题内容

这里有一个贪婪的小问题,希望这个问题也可以帮助其他想更多地了解注释验证的人

我目前正在学习Spring,现在,我计划尝试自定义带注释的验证。

我已经进行了很多搜索,现在我知道主要有两种验证,一种用于控制器,另一种是使用@Valid的注释方法

所以这是我的情况:假设我有两个或多个字段,当它们均为ALL
NULL时可以为null。但是,只有当这些字段之一包含空字符串以外的任何值时,才要求这些字段具有输入。我有两个想法,但不知道如何正确实施它们。

这是课程示例:

public class Subscriber {
    private String name;
    private String email;
    private Integer age;
    private String phone;
    private Gender gender;
    private Date birthday;
    private Date confirmBirthday;
    private String birthdayMessage;
    private Boolean receiveNewsletter;

    //Getter and Setter
}

假设我希望Birthday和confirmBirthday字段都必须为null或相反,那么我可能想为它们中的每一个使用一个注释来对其进行注释,如下所示:

public class Subscriber {
    private String name;
    private String email;
    private Integer age;
    private String phone;
    private Gender gender;

    @NotNullIf(fieldName="confirmBirthday")
    private Date birthday;

    @NotNullIf(fieldName="birthday")
    private Date confirmBirthday;

    private String birthdayMessage;
    private Boolean receiveNewsletter;

    //Getter and Setter
}

所以我确实需要像这样创建验证注释:

@Documented
@Constraint(validatedBy = NotNullIfConstraintValidator.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD })
public @interface NotNullIf {

    String fieldName();

    String message() default "{NotNullIf.message}";
    Class<?>[] group() default {};
    Class<? extends Payload>[] payload() default {};
}

然后,我将需要创建验证器本身:

public class NotNullIfConstraintValidator implements ConstraintValidator<NotNullIf, String>{

    private String fieldName;

    public void initialize(NotNullIf constraintAnnotation) {
        fieldName = constraintAnnotation.fieldName();
    }

    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(value == null) {
            return true;
        };
        //TODO Validation
        return false;
    }

}

对于使用同一类作为示例的另一个想法,例如说我想要生日,confirmBirthday和BirthdayMessdage只能为null或同时是反对的。这次我可能需要使用带注释的类进行跨字段验证。

这是我想注释课程的方式:

@NotNullIf(fieldName={"birthday", "confirmBirthday", "birthdayMessage"})
public class Subscriber {
    //Those field same as the above one
}

因此,当该字段之一不为null时,还需要根据客户端大小输入其余字段。可能吗?

我已阅读本文:如何访问注释属性中描述的字段

但是我仍然对上面列出的那些元素的注释验证如何工作感到困惑。也许我需要对该代码进行一些详细的说明,甚至更糟的是,我可能需要进行一些基本的概念检查。

请帮忙!


问题答案:

为此,您只能使用类型级别注释,因为字段级别注释无法访问其他字段!

我做了一些类似的事情以允许选择验证(确切地说,许多属性之一必须不为null)。在您的情况下,@AllOrNone注释(或您希望使用的任何名称)将需要一个字段名称数组,并且您将带注释类型的整个对象提供给验证器:

@Target(ElementType.TYPE)
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = AllOrNoneValidator.class)
public @interface AllOrNone {
    String[] value();

    String message() default "{AllOrNone.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

public class AllOrNoneValidator implements ConstraintValidator<AllOrNone, Object> {
    private static final SpelExpressionParser PARSER = new SpelExpressionParser();
    private String[] fields;

    @Override
    public void initialize(AllOrNone constraintAnnotation) {
        fields = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        long notNull = Stream.of(fields)
                .map(field -> PARSER.parseExpression(field).getValue(value))
                .filter(Objects::nonNull)
                .count();
        return notNull == 0 || notNull == fields.length;
    }
}

(正如您所说,您使用Spring时,我使用SpEL甚至允许嵌套字段访问)

现在您可以注释您的Subscriber类型:

@AllOrNone({"birthday", "confirmBirthday"})
public class Subscriber {
    private String name;
    private String email;
    private Integer age;
    private String phone;
    private Gender gender;
    private Date birthday;
    private Date confirmBirthday;
    private String birthdayMessage;
    private Boolean receiveNewsletter;
}


 类似资料:
  • 这里有一个小问题,希望这个问题也能帮助其他想知道更多关于注释验证的人 我目前正在研究Spring,目前,我计划尝试定制注释验证。 我搜索了很多,现在我知道主要有两种验证,一种用于控制器,另一种是使用@Valid的注释方法 在此之后,我需要创建验证器本身: 另一个想法是使用同一个类作为一个例子,它说我想要生日,confirmBirthday和birthdayMessdage只能同时为null或反对。

  • 我正在开发一个java spring mvc应用程序。我已经实现了一个自定义的验证注释来验证某些字符串(例如)。这是我的注释体: 这是我的类:

  • 我有一个简单的MVC控制器,我用我的自定义注释对它进行了注释: 我的自定义注释与一个方面链接,当验证不成功时,该方面会抛出。 所以现在的行为是,当验证失败时,我被重定向到500错误页面。我的目标是留在同一个页面并将拒绝的消息添加到: 我还没有找到这样做的方法,我找到的唯一方法是更改所有逻辑,不使用注释,同时在控制器代码中使用带有try/catch的验证服务。 当方面抛出这个异常时,有什么方法可以处

  • 我正在使用高级自定义字段为特定类别创建帖子。我使用Elementor(免费版)进行格式化,我希望使用ACF创建的所有帖子都应该显示在我的页面的特定部分下。但是,我不能这样做,因为elementor pro支持此功能,并且我不能用elementor编写php。 知道怎么做吗?

  • 问题内容: 我想使用猫鼬自定义验证来验证endDate是否大于startDate。如何访问startDate值?使用 this.startDate时 ,它不起作用;我不确定。 是比较两个日期的函数。 问题答案: 您可以尝试将日期戳嵌套在父对象中,然后验证父对象。例如:

  • 我需要在joomla 3.0的模块中使用xml创建一些自定义字段。我做了如下所示的事情。我的模块。字段的xml文件代码为: 如何保存多个字段的值?请帮帮我