2013-08-21 31 views
2

看看一個例子:註解的字段如何在組合的JSR-303註釋中設置消息?

@Pattern(regexp="[0-9]*") 
@Size(max =5) 
@Documented 
@Target({ANNOTATION_TYPE, METHOD, FIELD, CONSTRUCTOR, PARAMETER}) 
@Retention(RUNTIME) 
@Constraint(validatedBy = {}) //do not want any programmatic validation 
public @interface CustomAnnotation { 
    String message() default ""; 
    Class<?>[] groups() default {}; 
    Class<? extends Payload>[] payload() default {}; 
    } 
} 

例子:

@CustomAnnotation(message = "some important message") 
private String field; 

場時違反@Pattern我從@Pattern得到錯誤信息不是來自@CustomAnnotation。這是僅顯示@CustomAnnotation消息的方式嗎?

回答

2

您需要將meta-annotation @ReportAsSingleViolation添加到CustomConstraint的定義中。這樣,任何違反其組成約束條件的違規行爲都將被報告爲違反約束條件。參見BV參考文獻中的Constraint Composition部分。

相關問題