springboot表單提交之validator校驗(yàn)
表單提交是最常見的數(shù)據(jù)提交方式,我們經(jīng)常會(huì)填寫表單信息,比如用戶名,身份證,手機(jī)號(hào)等等,因此就會(huì)產(chǎn)生身份證是否合法,用戶名是否為空,雖然我們可以直接在前臺(tái)使用js就進(jìn)行格式的校驗(yàn),但如果使用postman工具直接發(fā)請(qǐng)求呢?使用我們后端也需要進(jìn)行對(duì)數(shù)據(jù)的校驗(yàn),這樣極大的確保數(shù)據(jù)的安全性和合法性。
1.新建一個(gè)Springboot項(xiàng)目,并且添加web依賴。下面是本項(xiàng)目的全部依賴<dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope></dependency><!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator --><dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.5.Final</version></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency> </dependencies>
這里涉及到幾個(gè)注解:
@NotEmpty(message=“用戶名不能為空”) @Length(min=6,max = 12,message=“用戶名長(zhǎng)度必須位于6到12之間”) @Email(message=“請(qǐng)輸入正確的郵箱”) @Pattern(regexp = “正則表達(dá)式”, message = “身份證格式錯(cuò)誤”)User.java的代碼:
package com.ctvit.validatordemo.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.hibernate.validator.constraints.Length;import javax.validation.constraints.Email;import javax.validation.constraints.NotEmpty;import javax.validation.constraints.Pattern;import java.io.Serializable;/** * 用戶名,密碼,郵箱,身份證 */@Data@AllArgsConstructor@NoArgsConstructorpublic class User implements Serializable { @NotEmpty(message='用戶名不能為空') @Length(min=6,max = 12,message='用戶名長(zhǎng)度必須位于6到12之間') private String userName; @NotEmpty(message='密碼不能為空') @Length(min=6,message='密碼長(zhǎng)度不能小于6位') private String passWord; @Email(message='請(qǐng)輸入正確的郵箱') private String email; @Pattern(regexp = '^(d{18,18}|d{15,15}|(d{17,17}[x|X]))$', message = '身份證格式錯(cuò)誤') private String idCard;}3.然后書寫一個(gè)簡(jiǎn)單的控制器來進(jìn)行模擬訪問:
package com.ctvit.validatordemo.controller;import com.ctvit.validatordemo.pojo.User;import org.springframework.validation.BindingResult;import org.springframework.validation.ObjectError;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import javax.validation.Valid;import java.util.List;@RestControllerpublic class UserController { @PostMapping('/Register') public String Register(@Valid User user, BindingResult bindingResult){//一個(gè)BindingResult對(duì)應(yīng)一個(gè)@Valid注解的參數(shù),用來表示校驗(yàn)消息StringBuffer stringBuffer = new StringBuffer();//使用StringBuffer拼接錯(cuò)誤信息,比如用戶名為空,長(zhǎng)度等if(bindingResult.hasErrors()){//判讀是否攜帶錯(cuò)誤信息 List<ObjectError> list =bindingResult.getAllErrors();//獲取所有錯(cuò)誤信息對(duì)象 for (ObjectError objectError:list) {//遍歷對(duì)象,獲取錯(cuò)誤的具體信息//將所有信息進(jìn)行拼接stringBuffer.append(objectError.getDefaultMessage());stringBuffer.append('---'); }}return stringBuffer!=null?stringBuffer.toString():'';//將錯(cuò)誤信息返回 }}4.這里涉及到一個(gè)接口:BindingResult。方法名 作用 hasErrors() 判斷當(dāng)前參數(shù)是否符合 getAllErrors() 獲取錯(cuò)誤信息對(duì)象 getDefaultMessage() 獲取錯(cuò)誤信息
輸入一半正確的,一般錯(cuò)誤的:
都正確的數(shù)據(jù):
到此這篇關(guān)于springboot表單提交之validator校驗(yàn)的文章就介紹到這了,更多相關(guān)springboot validator校驗(yàn)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. python中PyQuery庫(kù)用法分享3. python操作數(shù)據(jù)庫(kù)獲取結(jié)果之fetchone和fetchall的區(qū)別說明4. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)5. Ajax實(shí)現(xiàn)頁面無刷新留言效果6. python 爬取嗶哩嗶哩up主信息和投稿視頻7. 關(guān)于HTML5的img標(biāo)簽8. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總9. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效10. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能
