• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

101
Vistas
How can we use either of the validation in spring boot?

I have two variables in my bean and I want either name or mobile to be filled, they cant be both null at the same time.

@NotNull
private String name;

@NotNull
private String mobile;

How can I achieve that?

about 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You need to write a custom annotation for this and use on class

@AtLeastOneNotEmpty(fields = {"name", "phone"})
public class User{

Custom Annotation Implementation

@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 {};
}

And Validator of Custom Annotation

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);
  }
}
about 3 years ago · Santiago Trujillo Denunciar

0

you can create your own validation or annotation try like this :

@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 {};
}

and class that implement it:

   public class FieldConfirmedValidator implements ConstraintValidator<NotNullConfirmed, Object>{
    @Override
    public boolean isValid(Object user, ConstraintValidatorContext context) {
        String name = ((Your_bo)user).getName();
        String phone = ((Your_bo)user).getPhone();
        return !name.isEmpty() && !phone.isEmpty();
    }
}

and add this annotation to your class

@NotNullConfirmed 
public class User{
}
about 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda