[英]Meta-annotation with @Pattern not working in spring
注釋如下:
@Pattern(regexp = "^[0-9]+$")
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTestAnnotation {
}
當我用@MyTestAnnotion注釋一個字段時,即使該字段不包含所有數字,該呼叫也會順利進行。
以下部分已由以下給出的答案解決。 主要問題仍然存在。 我嘗試添加:
@Pattern(regexp = "^[0-9]+$")
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
public @interface MyTestAnnotation {
}
在我的@MyTestAnnotation上,它不會讓我輸入message屬性,但是當我運行代碼時,它給出了錯誤,指出未指定message屬性。
如果我將@Pattern批注直接放在字段上,則其行為將符合預期。
我在這里想念什么嗎?
您的注釋錯誤。 有關約束的最小屬性,請參見bean驗證規范的3.1.1節 ,以及有關如何進行適當組合的3.3節 。
您的注釋缺少message
, groups
和payload
屬性。
@Pattern(regexp = "^[0-9]+$")
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
public @interface MyTestAnnotation {
String message() default "{com.acme.constraint.MyConstraint.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
沒有這些屬性,您的注釋將不被視為約束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.