spring 注解驗證@NotNull等使用方法
本文介紹了spring 注解驗證@NotNull等使用方法,分享給大家,具體如下:
常用標(biāo)簽
@Null 被注釋的元素必須為null
@NotNull 被注釋的元素不能為null
@AssertTrue 被注釋的元素必須為true
@AssertFalse 被注釋的元素必須為false
@Min(value) 被注釋的元素必須是一個數(shù)字,其值必須大于等于指定的最小值
@Max(value) 被注釋的元素必須是一個數(shù)字,其值必須小于等于指定的最大值
@DecimalMin(value) 被注釋的元素必須是一個數(shù)字,其值必須大于等于指定的最小值
@DecimalMax(value) 被注釋的元素必須是一個數(shù)字,其值必須小于等于指定的最大值
@Size(max,min) 被注釋的元素的大小必須在指定的范圍內(nèi)。
@Digits(integer,fraction) 被注釋的元素必須是一個數(shù)字,其值必須在可接受的范圍內(nèi)
@Past 被注釋的元素必須是一個過去的日期
@Future 被注釋的元素必須是一個將來的日期
@Pattern(value) 被注釋的元素必須符合指定的正則表達式。
@Email 被注釋的元素必須是電子郵件地址
@Length 被注釋的字符串的大小必須在指定的范圍內(nèi)
@NotEmpty 被注釋的字符串必須非空
@Range 被注釋的元素必須在合適的范圍內(nèi)
example :
vo 頁面?zhèn)鬟^來的數(shù)據(jù)進行校驗
inferface : 只是作為標(biāo)記一個組別 可以在vo驗證的某個字段上面加入多個組別,這樣沒有加入的組別就不會驗證這個字段
controller: 需要 加入 @Validated (GroupInterface1.class) //GroupInterface1.class是定義的分組 GroupInterface2.class 需要校驗的字段是不會驗證的
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;
}
另外一個例子:
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 = "需要一個將來日期") // 只能是將來的日期
// @Past //只能去過去的日期
@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;
// 郵箱驗證
@Email(message = "郵箱格式錯誤")
private String email;
@Size(min = 20, max = 30, message = "字符串長度要求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 ****/
}
此時controller應(yīng)該要加上@Valid ,否則不會驗證!
/***
* 解析驗證參數(shù)錯誤
* @param vp —— 需要驗證的POJO,使用注解@Valid 表示驗證
* @param errors 錯誤信息,它由Spring MVC通過驗證POJO后自動填充
* @return 錯誤信息Map
*/
@RequestMapping(value = "/valid/validate")
@ResponseBody
public Map<String, Object> validate(
@Valid @RequestBody ValidatorPojo vp, Errors errors) {
Map<String, Object> errMap = new HashMap<>();
// 獲取錯誤列表
List<ObjectError> oes = errors.getAllErrors();
for (ObjectError oe : oes) {
String key = null;
String msg = null;
// 字段錯誤
if (oe instanceof FieldError) {
FieldError fe = (FieldError) oe;
key = fe.getField();// 獲取錯誤驗證字段名
} else {
// 非字段錯誤
key = oe.getObjectName();// 獲取驗證對象名稱
}
// 錯誤信息
msg = oe.getDefaultMessage();
errMap.put(key, msg);
}
return errMap;
}
到此這篇關(guān)于spring 注解驗證@NotNull等使用方法的文章就介紹到這了,更多相關(guān)spring 注解驗證 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 使用JdbcTemplate 中的queryForList發(fā)生錯誤解決辦法
這篇文章主要介紹了Java 使用JdbcTemplate 中的queryForList發(fā)生錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-07-07
Docker容器使用宿主機上的mongod/redis等服務(wù)詳解
這篇文章主要介紹了Docker容器使用宿主機上的mongod/redis等服務(wù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
springboot集成CAS實現(xiàn)單點登錄的示例代碼
這篇文章主要介紹了springboot集成CAS實現(xiàn)單點登錄的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
spring cloud-給Eureka Server加上安全的用戶認(rèn)證詳解
這篇文章主要介紹了spring cloud-給Eureka Server加上安全的用戶認(rèn)證詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
SpringBoot?ScheduledTaskRegistrar解決動態(tài)定時任務(wù)思路詳解
本文將從問題出發(fā),詳細介紹ScheduledTaskRegistrar類是如何解決動態(tài)調(diào)整定時任務(wù)的思路,并給出關(guān)鍵的代碼示例,幫助大家快速地上手學(xué)習(xí)2023-02-02
IntelliJ IDEA運行bat腳本,自動taskkill端口進程
這篇文章主要介紹了IDEA里面無法運行bat文件的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot與SpringMVC中參數(shù)傳遞的原理解析
這篇文章主要介紹了SpringBoot與SpringMVC中參數(shù)傳遞的原理,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07

