Validation of method parameters of spring beans (@Controller
, @Service
etc)
spring-boot-starter-web
:
org.hibernate.validator:hibernate-validator
javax.validation:validation-api
Spring Boot provides the required infrastructure beans org.springframework.validation.beanvalidation.MethodValidationPostProcessor
and org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
.
The annotation for the validation, for instance@MyValidationConstraint
, must have the attributes message
, groups
and payload
. For details see javadoc of javax.validation.Constraint
.
@Target({ ElementType.METHOD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MyValidationConstraintValidator.class)
// https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/?v=6.0#validator-customconstraints-simple
public @interface MyValidationConstraint {
// message interpolation: see https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/?v=6.0#chapter-message-interpolation
String message() default "The value ${validatedValue} is not valid.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
The validator MyValidationConstraintValidator
can be implemented as a spring bean:
@Component
public class MyValidationConstraintValidator implements ConstraintValidator<MyValidationConstraint, String> {
private MyService myService;
public StockSymbolConstraintValidator(final MyService myService) {
this.myService = myService;
}
@Override
public boolean isValid(final String value, final ConstraintValidatorContext context) {
// can use myService to test value
return true or false;
}
}
The bean which method parameter should be validatet must be annotated with @org.springframework.validation.annotation.Validated
. The method parameter to check must be annotated with MyValidationConstraint
.
@Service
@Validated
public class SomeService {
public ReturnObject serviceMethod(@MyValidationConstraint String symbol) {
...
}
}