There is a controller accepting code as a path variable
@RestController
@RequestMapping(value = "/api/currency")
@Validated
public class CurrencyController {
@GetMapping("/gif/{code}")
public ResponseEntity<Map> getChangeGif(@PathVariable @Code String code){
// some implementation
return null;
}
}
I want to use my own annotation to validate code as I want
@Target( { FIELD, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = CodeValidator.class)
public @interface Code {
public String message() default "error message";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}
And here is the validator
public class CodeValidator implements ConstraintValidator<Code, String> {
@Override
public void initialize(Code constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}
@Override
public boolean isValid(String code, ConstraintValidatorContext context) {
// validator implementation
return false;
}
}
For some reason when requests come, validation just skipps, and controller continue working without it
@Length(min=2)etc