我有以下使用 nameId 作為 RequestHeader 的方法,還有其他使用相同變數宣告的方法。有沒有辦法可以將nameId驗證分組到一個界面中,以便我們可以注釋而不是每次都添加@Valid & @Pattern?
public ResponseEntity<PlanResponse> updatePlanProductByPlanId(
@RequestHeader(value = "name_id", required = true) @Valid @Pattern(regexp = REGEX_PATTERN_ALPHANUMERIC, message = "{error.invalid.name.id.header}") String nameId,
@PathVariable(value = "planid") @Valid @Pattern(regexp = REGEX_PATTERN_ONLY_NUMBERS, message = "{error.invalid.plan.id}") String planId,
@Validated({
OnPlanProductUpdate.class }) @RequestBody(required = true) SavePlanProductRequest updateProductRequest)
throws ServiceException { ... }
期待像@NameId注釋這樣的東西。
public ResponseEntity<PlanResponse> updatePlanProductByPlanId(
@RequestHeader(value = "name_id", required = true) @NameId String nameId,
@PathVariable(value = "planid") @Valid @Pattern(regexp = REGEX_PATTERN_ONLY_NUMBERS, message = "{error.invalid.plan.id}") String planId,
@Validated({
OnPlanProductUpdate.class }) @RequestBody(required = true) SavePlanProductRequest updateProductRequest)
throws ServiceException { ... }
我嘗試創建一個如下所示的界面,但驗證似乎不起作用。
@Valid
@Pattern(regexp = REGEX_PATTERN_ALPHANUMERIC, message = "{error.invalid.name.id.header}")
@Retention(RetentionPolicy.RUNTIME)
public @interface NameId {
String nameId() default "";
}
uj5u.com熱心網友回復:
這應該作業
@Pattern(regexp = REGEX_PATTERN_ALPHANUMERIC, message = "{error.invalid.name.id.header}")
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {})
public @interface NameId {
String nameId() default "";
}
我認為缺少約束,不需要有效注釋。
在此處閱讀更多資訊:https ://www.baeldung.com/java-bean-validation-constraint-composition
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/531732.html
上一篇:為什么SpringBoot2.7.5使用SQLServerJRE8驅動程式?
下一篇:將字符存盤在二維陣列中并列印
