Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我们如何在spring boot中使用这两种验证?_Java_Mysql_Spring_Validation_Jpa - Fatal编程技术网

Java 我们如何在spring boot中使用这两种验证?

Java 我们如何在spring boot中使用这两种验证?,java,mysql,spring,validation,jpa,Java,Mysql,Spring,Validation,Jpa,我的bean中有两个变量,我希望填充name或mobile,它们不能同时为null @NotNull private String name; @NotNull private String mobile; 如何实现这一点?您可以创建自己的验证或注释 试着这样做: @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface NotNullConfirmed { String mes

我的bean中有两个变量,我希望填充name或mobile,它们不能同时为null

@NotNull
private String name;

@NotNull
private String mobile;

如何实现这一点?

您可以创建自己的验证或注释 试着这样做:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotNullConfirmed {
    String message() default "they can not be null";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

您需要为此编写自定义注释并在类上使用

@AtLeastOneNotEmpty(fields = {"name", "phone"})
public class User{
自定义注释实现

@Constraint(validatedBy = AtLeastOneNotEmptyValidator.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AtLeastOneNotEmpty {

  String message() default "At least one cannot be null";

  String[] fields();

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

  Class<? extends Payload>[] payload() default {};
}
@Constraint(validatedBy=atlestonenotemptyvalidator.class)
@目标({ElementType.TYPE})
@保留(RetentionPolicy.RUNTIME)
public@interface atlestonenotempty{
String message()默认为“至少一个不能为空”;
字符串[]字段();
类[]组()默认值{};

类这是否回答了您的问题?您的验证器如何与注释连接,以及您的\u bo在这里是什么?
@AtLeastOneNotEmpty(fields = {"name", "phone"})
public class User{
@Constraint(validatedBy = AtLeastOneNotEmptyValidator.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AtLeastOneNotEmpty {

  String message() default "At least one cannot be null";

  String[] fields();

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

  Class<? extends Payload>[] payload() default {};
}
public class AtLeastOneNotEmptyValidator
    implements ConstraintValidator<AtLeastOneNotEmpty, Object> {

  private String[] fields;

  public void initialize(AtLeastOneNotEmpty constraintAnnotation) {
    this.fields = constraintAnnotation.fields();
  }

  public boolean isValid(Object value, ConstraintValidatorContext context) {

    List<String> fieldValues = new ArrayList<String>();

    for (String field : fields) {
      Object propertyValue = new BeanWrapperImpl(value).getPropertyValue(field);
      if (ObjectUtils.isEmpty(propertyValue)) {
        fieldValues.add(null);
      } else {
        fieldValues.add(propertyValue.toString());
      }
    }
    return fieldValues.stream().anyMatch(fieldValue -> fieldValue!= null);
  }
}