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è)過去的日期@Future 被注釋的元素必須是一個(gè)將來的日期@Pattern(value) 被注釋的元素必須符合指定的正則表達(dá)式。@Email 被注釋的元素必須是電子郵件地址@Length 被注釋的字符串的大小必須在指定的范圍內(nèi)@NotEmpty 被注釋的字符串必須非空@Range 被注釋的元素必須在合適的范圍內(nèi)
example :vo 頁面?zhèn)鬟^來的數(shù)據(jù)進(jìn)行校驗(yàn)inferface : 只是作為標(biāo)記一個(gè)組別 可以在vo驗(yàn)證的某個(gè)字段上面加入多個(gè)組別,這樣沒有加入的組別就不會(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è)將來日期') // 只能是將來的日期 // @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; // 郵箱驗(yàn)證 @Email(message = '郵箱格式錯(cuò)誤') 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 ****/}
此時(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通過驗(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)證對象名稱 } // 錯(cuò)誤信息 msg = oe.getDefaultMessage(); errMap.put(key, msg); } return errMap; }
到此這篇關(guān)于spring 注解驗(yàn)證@NotNull等使用方法的文章就介紹到這了,更多相關(guān)spring 注解驗(yàn)證 內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 前端從瀏覽器的渲染到性能優(yōu)化2. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁3. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)4. 讀大數(shù)據(jù)量的XML文件的讀取問題5. 解析原生JS getComputedStyle6. PHP循環(huán)與分支知識(shí)點(diǎn)梳理7. css代碼優(yōu)化的12個(gè)技巧8. 利用CSS3新特性創(chuàng)建透明邊框三角9. ASP實(shí)現(xiàn)加法驗(yàn)證碼10. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))
