在转换为布尔类型之前验证@QueryParam变量

Validation @QueryParam variable before conversion into boolean type

提问人:xeqer 提问时间:11/9/2023 最后编辑:xeqer 更新时间:11/9/2023 访问量:19

问:

我想在方法中验证@QueryParams参数,然后再解析为布尔类型(如果值不是 true/false,它应该抛出错误而不是解析为 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;
    }
}

但它以错误结束(因为在解析从 url 检索到的字符串后,对布尔类型进行了验证)

有没有很好的方法可以实现这一点?

爪哇 java-8 java-ee-6

评论


答: 暂无答案