spring 注解驗(yàn)證@NotNull等使用方法
本文介紹了spring 注解驗(yàn)證@NotNull等使用方法,分享給大家,具體如下:
常用標(biāo)簽
@Null 被注釋的元素必須為null
@NotNull 被注釋的元素不能為null
@AssertTrue 被注釋的元素必須為true
@AssertFalse 被注釋的元素必須為false
@Min(value) 被注釋的元素必須是一個(gè)數(shù)字,其值必須大于等于指定的最小值
@Max(value) 被注釋的元素必須是一個(gè)數(shù)字,其值必須小于等于指定的最大值
@DecimalMin(value) 被注釋的元素必須是一個(gè)數(shù)字,其值必須大于等于指定的最小值
@DecimalMax(value) 被注釋的元素必須是一個(gè)數(shù)字,其值必須小于等于指定的最大值
@Size(max,min) 被注釋的元素的大小必須在指定的范圍內(nèi)。
@Digits(integer,fraction) 被注釋的元素必須是一個(gè)數(shù)字,其值必須在可接受的范圍內(nèi)
@Past 被注釋的元素必須是一個(gè)過(guò)去的日期
@Future 被注釋的元素必須是一個(gè)將來(lái)的日期
@Pattern(value) 被注釋的元素必須符合指定的正則表達(dá)式。
@Email 被注釋的元素必須是電子郵件地址
@Length 被注釋的字符串的大小必須在指定的范圍內(nèi)
@NotEmpty 被注釋的字符串必須非空
@Range 被注釋的元素必須在合適的范圍內(nèi)
example :
vo 頁(yè)面?zhèn)鬟^(guò)來(lái)的數(shù)據(jù)進(jìn)行校驗(yàn)
inferface : 只是作為標(biāo)記一個(gè)組別 可以在vo驗(yàn)證的某個(gè)字段上面加入多個(gè)組別,這樣沒(méi)有加入的組別就不會(huì)驗(yàn)證這個(gè)字段
controller: 需要 加入 @Validated (GroupInterface1.class) //GroupInterface1.class是定義的分組 GroupInterface2.class 需要校驗(yàn)的字段是不會(huì)驗(yàn)證的
VO:
public class User implements Serializable {
/**
* 主鍵
*/
@NotNull(message = "primary is not null",groups = {GroupInterface1.class})
private Long id;
@Pattern(regexp = "[0123456789]",groups = {GroupInterface1.class,GroupInterface2.class},message = "hava a error Date")
private Long maxDiscountAmount;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
@Future(message = "expireTime is not less than now",groups = {GroupInterface1.class,GroupInterface2.class})
@NotNull(message = "expireTime is not null",groups = {GroupInterface1.class,GroupInterface2.class})
private Date expireTime;
}
另外一個(gè)例子:
import java.util.Date;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Email;
import javax.validation.constraints.Future;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;
/**** imports ****/
public class ValidatorPojo {
// 非空判斷
@NotNull(message = "id不能為空")
private Long id;
@Future(message = "需要一個(gè)將來(lái)日期") // 只能是將來(lái)的日期
// @Past //只能去過(guò)去的日期
@DateTimeFormat(pattern = "yyyy-MM-dd") // 日期格式化轉(zhuǎn)換
@NotNull // 不能為空
private Date date;
@NotNull // 不能為空
@DecimalMin(value = "0.1") // 最小值0.1元
@DecimalMax(value = "10000.00") // 最大值10000元
private Double doubleValue = null;
@Min(value = 1, message = "最小值為1") // 最小值為1
@Max(value = 88, message = "最大值為88") // 最大值88
@NotNull // 不能為空
private Integer integer;
@Range(min = 1, max = 888, message = "范圍為1至888") // 限定范圍
private Long range;
// 郵箱驗(yàn)證
@Email(message = "郵箱格式錯(cuò)誤")
private String email;
@Size(min = 20, max = 30, message = "字符串長(zhǎng)度要求20到30之間。")
private String size;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Double getDoubleValue() {
return doubleValue;
}
public void setDoubleValue(Double doubleValue) {
this.doubleValue = doubleValue;
}
public Integer getInteger() {
return integer;
}
public void setInteger(Integer integer) {
this.integer = integer;
}
public Long getRange() {
return range;
}
public void setRange(Long range) {
this.range = range;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
/**** setter and getter ****/
}
此時(shí)controller應(yīng)該要加上@Valid ,否則不會(huì)驗(yàn)證!
/***
* 解析驗(yàn)證參數(shù)錯(cuò)誤
* @param vp —— 需要驗(yàn)證的POJO,使用注解@Valid 表示驗(yàn)證
* @param errors 錯(cuò)誤信息,它由Spring MVC通過(guò)驗(yàn)證POJO后自動(dòng)填充
* @return 錯(cuò)誤信息Map
*/
@RequestMapping(value = "/valid/validate")
@ResponseBody
public Map<String, Object> validate(
@Valid @RequestBody ValidatorPojo vp, Errors errors) {
Map<String, Object> errMap = new HashMap<>();
// 獲取錯(cuò)誤列表
List<ObjectError> oes = errors.getAllErrors();
for (ObjectError oe : oes) {
String key = null;
String msg = null;
// 字段錯(cuò)誤
if (oe instanceof FieldError) {
FieldError fe = (FieldError) oe;
key = fe.getField();// 獲取錯(cuò)誤驗(yàn)證字段名
} else {
// 非字段錯(cuò)誤
key = oe.getObjectName();// 獲取驗(yàn)證對(duì)象名稱
}
// 錯(cuò)誤信息
msg = oe.getDefaultMessage();
errMap.put(key, msg);
}
return errMap;
}
到此這篇關(guān)于spring 注解驗(yàn)證@NotNull等使用方法的文章就介紹到這了,更多相關(guān)spring 注解驗(yàn)證 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于QueryWrapper,實(shí)現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢方式
這篇文章主要介紹了關(guān)于QueryWrapper,實(shí)現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01
教你利用JAVA實(shí)現(xiàn)可以自行關(guān)閉服務(wù)器的方法
今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著利用JAVA實(shí)現(xiàn)可以自行關(guān)閉服務(wù)器的方法展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Spring Cloud Feign 自定義配置(重試、攔截與錯(cuò)誤碼處理) 代碼實(shí)踐
這篇文章主要介紹了Spring Cloud Feign 自定義配置(重試、攔截與錯(cuò)誤碼處理) 實(shí)踐,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Spring Boot命令行啟動(dòng)添加參數(shù)的三種方式
在命令行中,常見(jiàn)的參數(shù)可以分為三類:選項(xiàng)參數(shù)、非選項(xiàng)參數(shù)和系統(tǒng)參數(shù),本文就來(lái)介紹一下Spring Boot命令行三種參數(shù)形式,感興趣的可以了解一下2023-09-09

