java 参数校验注解合并组合

原文: https://www.v2ex.com/t/547614

  1. 使用 @ReportAsSingleViolation 注解
1
2
3
4
5
6
7
@ReportAsSingleViolation
@Pattern(regexp = "^[\\u4e00-\\u9fa5\\w]+$")
@Constraint(validatedBy = {})
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
public @interface ChsNamePattern {}
  1. 使用 @OverridesAttribute 注解
1
2
3
4
5
6
7
8
9
10
@Pattern(regexp = "^[\\u4e00-\\u9fa5\\w]+$")
@Constraint(validatedBy = {})
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
public @interface ChsNamePattern {

@OverridesAttribute(constraint = Pattern.class, name = "message")
String message() default "自定义 message";
}

https://beanvalidation.org/1.0/spec/#constraintsdefinitionimplementation-constraintcomposition