簡體   English   中英

使用 Spring Boot 中的注釋驗證 POJO 中的嵌套 JSON

[英]Validate nested JSON in POJO using annotation in Spring boot

我有一個我想驗證的 JSON。

{
  "a": {
    "attribute1": "",
    "attribute2": ""
  },
  "b": {
    "attribute3": "1240717"
  },
  "c": {
    "attribute4": "1321"
  },
  "d": "Lab",
  "c": {
    "attribute5": "2021-09-06T20:42:48",
    "attribute6": "2021-09-06T20:42:48"
  }
}

我想在嵌套的屬性 3、屬性 4、屬性 5 和屬性 6 上的 POJO 類中使用 @NotNull 檢查 NotNull 條件。 我試過這個

public class JsonValidator {
    
    @NonNull
    @javax.validation.constraints.NotBlank
    @NotEmpty(message = "Bad request")
    private String attribute3;
    @NonNull
    @javax.validation.constraints.NotBlank
    @NotEmpty
    private String attribute4;
    @NonNull
    @javax.validation.constraints.NotBlank
    @NotEmpty
    private String attribute5;
    @NonNull
    @javax.validation.constraints.NotBlank
    @NotEmpty
    private String attribute6;

但它僅在屬性未嵌套在另一個對象中且存在於根對象中時才有效。 誰能告訴我如何實現這一目標

您必須使用 @Valid 注釋來注釋您的復雜對象。 這將 y=then 觸發嵌套屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM