Validation @QueryParam variable before conversion into boolean type

26 views Asked by At

I would like to to validate @QueryParams parameter in method before it would parse to Boolean type (in case when value is not true/false it should throw an error instead of parse to false).

Something like that -> 
public Response restMethod(
@CustomValidation @QueryParam("param") Boolean myParam)```


I have problem to deal with this. Tried something like 
`@Target(
    {
        ElementType.TYPE,
        ElementType.METHOD,
        ElementType.FIELD,
        ElementType.PARAMETER
    }
)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(
    validatedBy = {
        BooleanValidator.class
    }
)
public @interface IsValidBooleanValue {
    
    String message() default "xxxxxxxxxxx";

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

    Class<? extends Payload>[] payload() default {};
}
`
`public class BooleanValidator implements ConstraintValidator<IsValidBooleanValue, String> {

    @Override
    public void initialize(IsValidBooleanValue constraintAnnotation) {}

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

But it ends with error (because validation is done on Boolean type after parsing String retrieved from url)

Is there a good way to implement this?

0

There are 0 answers