簡體   English   中英

休息 api 的條件驗證

[英]Conditional validation on rest api

我有一個 api,其中包含一個接受對象作為請求正文的方法。 我想做的是應用條件驗證。 看看下面的代碼:

@Override
@PostMapping(value = "/private/getUsers/",
        produces = {"application/json"},
        consumes = {"application/json"})
public ResponseEntity<Object> getUsers(@ApiParam(value = "Request object for users", required = true)
                                                 @Valid @RequestBody USerType userType) {

   //do stuff here
    return new ResponseEntity<>(Object, HttpStatus.OK);
}

class UserType {
  TypeEnum typeEnum
  String name;
  String adminId    

 //getters and setters
}

因此,如果傳遞的 UserType 對象包含一個等於 admin 的 TypeEnum,我想進行驗證以確保傳遞 adminID,如果傳入的 TypeEnum 是收銀員類型,我不想強​​制執行此驗證

您可以使用@AssertTrue添加自定義@AssertTrue 試試這個類:

class UserType {
  TypeEnum typeEnum
  String name;
  String adminId    

 //getters and setters

@AssertTrue
    public boolean isValid() {
        if (TypeEnum.admin == typeEnum) {
            if (check admin id not blank){
                return true;
            }
            return false;
        }
        return true;
    }
}

暫無
暫無

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

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