所以我的示例测试项目将显示完整的相关配置和代码:
约束注释:
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FooValidator.class)
public @interface FooValid {
String message();
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)
注释PoJo:
public class Foo {
@FooValid(message = "Test failed")
private Integer test;
[...]
}
Run Code Online (Sandbox Code Playgroud)
使用@Validated进行带注释的服务:
@Service
@Validated
public class FooService {
private final Test test;
@Autowired
public FooService(final Test test) {
this.test = test;
}
public void foo(@Valid final Foo foo) {
this.test.test(foo);
}
}
Run Code Online (Sandbox Code Playgroud)
JSR-303 ConstraintValidator:
public …Run Code Online (Sandbox Code Playgroud)