SpringMVC 使用JSR-303进行校验Bean Validation------组合验证注解

有时候,可能有好几个注解需要一起使用,此时就可以使用组合验证注解

@Target({ FIELD})  
@Retention(RUNTIME)  
@Documented  
@NotNull(message = "{user.name.null}")  
@Length(min = 5, max = 20, message = "{user.name.length.illegal}")  
@Pattern(regexp = "[a-zA-Z]{5,20}", message = "{user.name.length.illegal}")  
@Constraint(validatedBy = { })  
public @interface Composition {  
    String message() default "";  
    Class<?>[] groups() default { };  
    Class<? extends Payload>[] payload() default { };  
}  

这样我们验证时只需要

@Composition()  
private String name; 

简洁多了。

想了解更多java相关技术,请关注公众号“JavaEE那些事”

扫描下面二维码,更多技术资料等你来拿
这里写图片描述

猜你喜欢

转载自blog.csdn.net/forwujinwei/article/details/79363430