Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
¿Cómo podemos usar cualquiera de las validaciones en Spring Boot?

Tengo dos variables en mi bean y quiero que se llene el nombre o el móvil, no pueden ser ambos nulos al mismo tiempo.

 @NotNull private String name; @NotNull private String mobile;

¿Cómo puedo lograr eso?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe escribir una anotación personalizada para esto y usarla en clase

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

Implementación de anotaciones personalizadas

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

Y Validador de Anotación Personalizada

 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); } }
over 4 years ago · Santiago Trujillo Report

0

puede crear su propia validación o anotación, intente así:

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

y clase que lo implementan:

 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(); } }

y agrega esta anotación a tu clase

 @NotNullConfirmed public class User{ }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!