'Dropwizard: 1.X - can I get cross-parameter method validation to work?

Annotation:

@Target({METHOD, CONSTRUCTOR, PARAMETER, FIELD})
@Documented
@Constraint(validatedBy = AtLeastOneNotNullValidator.class)
@Retention(RUNTIME)
public @interface AtLeastOneNotNull {

    String message () default "{com.validationError}";

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

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

    String[] fieldNames();
}

Validator (currently always returns false on purpose):

@SupportedValidationTarget(ValidationTarget.PARAMETERS)
public class AtLeastOneNotNullValidator implements ConstraintValidator<AtLeastOneNotNull, String[]> {

    private String[] fieldNames;

    @Override
    public void initialize(AtLeastOneNotNull constraintAnnotation) {
        this.fieldNames = constraintAnnotation.fieldNames();
    }

    @Override
    public boolean isValid(String[] value, ConstraintValidatorContext context) {
        return false;
    }

}

Method:

    // within some class
    @Valid
    @AtLeastOneNotNull(fieldNames = {"userName", "companyName"})
    private List<SomeObj> findAll(@Valid String userName, @Valid String companyName, String token) {...}

No matter what I can't seem to get this to work. What am I missing?



Solution 1:[1]

it worked for me by adding @Validated(org.springframework.validation.annotation.Validated;) before the class declaration.

e.g.

   @Service
   @Validated
   public class CourseService {
   ...
   }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yupichkin