我有一个REST控制器,我想验证一个标头参数。 我已经为它创建了一个自定义注释。但是验证没有被调用。我们在控制器上添加了@Validated注释
如何验证标题中的参数?
控制器方法:
@RestController
@RequestMapping("/ids/{id}")
@Validated
public class IdController {
@RequestMapping(value = "/", method = POST, produces = APPLICATION_JSON_VALUE)
public ResponseEntity accept(@PathVariable(value = "id") String id,
@Valid @RequestBody Request request,
@RequestHeader(value = mode) @ValidMode String mode) {
}
}
注释:
@Target({FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Constraint(validatedBy = ModeValidator.class)
public @interface ValidMode {
String message() default "incorrect mode";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Validator
public class ModeValidator implements ConstraintValidator<ValidMode, String> {
@Override
public void initialize(ValidMode constraint) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return false;
}
}
我可以验证请求标头模式吗?