JSR 303驗證更新資訊

2021-10-09 14:30:42 字數 2816 閱讀 6228

@apimodelproperty

(value=

"使用者名稱"

, name=

"username"

, example=

"json"

, required =

false

)private string username;

@apimodelproperty

(value=

"密碼"

, name=

"password"

, example=

"123456"

, required =

false

)private string password;

@apimodelproperty

(value=

"確認密碼"

, name=

"confirmpassword"

, example=

"123456"

, required =

false

)private string confirmpassword;

@notblank

(message =

"使用者暱稱不能為空"

)@length

(max =

12, message =

"使用者暱稱不能超過12位"

)@apimodelproperty

(value=

"使用者暱稱"

, name=

"nickname"

, example=

"傑森"

, required =

false

)private string nickname;

@length

(max =

12, message =

"使用者真實姓名不能超過12位"

)@apimodelproperty

(value=

"真實姓名"

, name=

"realname"

, example=

"傑森"

, required =

false

)private string realname;

@pattern

(regexp =

"^(((13[0-9])|(15[0-9])|(18[0-9]))+\\d)$"

, message =

"手機號格式不正確"

)@apimodelproperty

(value=

"手機號"

, name=

"mobile"

, example=

"13999999999"

, required =

false

)private string mobile;

@email

@apimodelproperty

(value=

"郵箱位址"

, name=

"email"

, example=

"[email protected]"

, required =

false

)private string email;

@min

(value =

0, message =

"性別選擇不正確"

)@max

(value =

2, message =

"性別選擇不正確"

)@apimodelproperty

(value=

"性別"

, name=

"***"

, example=

"0:女 1:男 2:保密"

, required =

false

)private integer ***;

@apimodelproperty

(value=

"生日"

, name=

"birthday"

, example=

"1900-01-01"

, required =

false

)private date birthday;

定義好bo,controller 進行驗證客戶端訊息

public r update

(@apiparam

(name =

"userid"

, value =

"使用者id"

, required =

true

)@requestparam string userid,

@requestbody

@valid centeruserbo centeruserbo,

bindingresult result)

return r.

error

(usererrorcode.update_user_infor_error,errormap);}

users users = centeruserservice.

updateuserinfo

(userid, centeruserbo);if

(users != null)

return r.

error()

;}

驗證結果:

JSR 303約束規則

asserttrue assertfalse 驗證適用字段 boolean 註解說明 驗證值是否為 true false 屬性說明 decimalmax decimalmin 驗證適用字段 bigdecimal,biginteger,string,byte,short,int,long 註解說明 驗...

筆記 JSR303 表單校驗

要在應用校驗,需要 1.宣告校驗規則 data public class taco data public class order 2.在表單繫結的時候執行校驗 新增了 valid註解對taco物件進行校驗和errors屬性判斷是否出錯。public string processdesign val...

JSR303後端校驗(一)

jsr303後端校驗 一 1 在pom檔案中新增依賴 dependency groupid org.hibernate groupid artifactid hibernate validator artifactid version 6.0.16.final version dependency ...