我正在開發一個使用spring-boot的應用程序。我想使用JSR註釋來驗證用戶bean。問題是我有一些領域取決於他人的價值。例如,當status =「user_pr」時,我必須將地址,縣和電話號碼設置爲強制性的。自定義bean驗證
這是我的豆:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
@NotNull(message = "required")
private String status;
@JsonProperty("first_name")
@NotNull(message = "required")
private String firstName;
@NotNull(message = "required")
private String name;
@NotNull(message = "required")
@Pattern(message = "Email not valid", regexp = "^([\\w\\.\\-_]+)?\\[email protected][\\w-_]+(\\.\\w+){1,}$")
private String mailAddress;
private String country;
private String phoneNumber;
@JsonProperty("address")
private Address billingAddress;
}
請問您有什麼想法?
問候
檢查Spring驗證文檔:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html – Sigrist