引數校驗框架 自定義校驗註解

2021-10-22 00:21:36 字數 1693 閱讀 3662

一、場景介紹

二、校驗場景

三、定義註解

四、使用註解

新建spring boot專案

建立訂單狀態列舉型別

@getter

public

enum orderstateenum

/** 根據**獲取列舉名稱*/

public

static string getnamebycode

(string code)

}return null;

}/** 根據名稱獲取列舉***/

public

static string getcodebyname

(string name)

}return null;

}/** 根據**獲取列舉物件*/

public

static orderstateenum getorderstateenumbycode

(string code)

}return null;

}/** 根據名稱獲取列舉物件*/

public

static orderstateenum getorderstateenumbyname

(string name)

}return null;

}}

建立order實體

@data

@tostring

public

class

order

implements

serializable

, message =

"只能查詢指定狀態的訂單資訊-1"

)private string orderstate;

/** 訂單狀態列舉

定製化註解,實現引數值與列舉列表的自動匹配校驗(能更好地與實際業務開發匹配)*/

@enumvalidated

(enumvalues = orderstateenum.

class

, message =

"只能查詢指定狀態的訂單資訊-2"

)private string orderstateenum;

}

p.s

建立介面控制器

("/order"

)@apioperation

(value =

"訂單查詢"

, notes =

"訂單查詢,匹配自定義驗證註解,丟擲異常由統一異常處理"

)public dataresult orderlist

(@requestbody

@validated order order)

", order.

tostring()

);return dataresult.

success()

;}模擬請求引數

驗證響應結果

p.s

以上controller通過專案中定義@restcontrolleradvice來進行異常統一處理,所以看到的響應結果是封裝過的

驗證註解,自定義校驗註解

session的token時間併發的session數量,集群如何管理 notblank valid bindingresult public listgetuser requestparam string username,bindingresult errors return list hiber...

Springboot自定義校驗註解

max 10 最大不能超過10 max value 10,message 資料最大不能超過10 最大不能超過10 min range min 1,max 10 範圍 1 10 length min 2,max 10 輸入的長度 最小為2,最大為10 需要建乙個註解類和關聯類 1.target ele...

自定義註解校驗API引數電話號

在建立註解之前,我們首先明確 號碼校驗邏輯,要實現 手機號碼不能為空 符合手機號的正規表示式 號碼的長度校驗 documented constraint validatedby phonenumbervalidator.class target retention retentionpolicy.r...