1、在進行Web專案開發的程序中,用戶提交資料的合法性是最基礎的驗證手段,在SpringBoot中可以直接使用hibernate-vidator組件包實作驗證處理,而此組件包中支持的驗證注解,如圖所示,
2、在src/main/resources目錄下創建ValidationMessages.properties(檔案名稱為默認設定,不可更改)檔案,該檔案中要保留所有的錯誤提示資訊,
1 userinfo.username.notnull.error=用戶賬號不能為空!
2 userinfo.email.from.error=請輸入正確格式的用戶郵箱!
3 userinfo.username.length.error=用戶賬號長度錯誤!
4 userinfo.age.notnull.error=用戶年齡不能為空!
5 userinfo.age.digits.error=用戶年齡必須是合法的數字!
6 userinfo.salary.notnull.error=用戶工資不能為空!
7 userinfo.salary.digits.error=用戶工資必須是合法的數字!
8 userinfo.birthday.notnull.error=用戶生日不允許為空!
建立一個UserInfo物體類,并且在該類上使用驗證注解,同時,驗證出錯時的錯誤資訊參考之前ValidationMessages.properties檔案中的定義,
1 package com.demo.po;
2
3 import java.io.Serializable;
4 import java.util.Date;
5
6 import javax.validation.constraints.Digits;
7 import javax.validation.constraints.Email;
8 import javax.validation.constraints.NotNull;
9
10 /**
11 *
12 * @author
13 *
14 */
15 public class UserInfo implements Serializable {
16
17 /**
18 *
19 */
20 private static final long serialVersionUID = 1L;
21
22 @NotNull(message = "{userinfo.username.notnull.error}")
23 private String userName;
24
25 @Email(message = "{userinfo.email.from.error}")
26 private String email;
27
28 @NotNull(message = "{userinfo.age.notnull.error}")
29 @Digits(integer = 3, fraction = 0, message = "{userinfo.age.digits.error}")
30 private Integer age;
31
32 @NotNull(message = "{userinfo.salary.notnull.error}")
33 @Digits(integer = 20, fraction = 2, message = "{userinfo.salary.digits.error}")
34 private Integer salary;
35
36 @NotNull(message = "{userinfo.birthday.notnull.error}")
37 private Date birthday;
38
39 public String getUserName() {
40 return userName;
41 }
42
43 public void setUserName(String userName) {
44 this.userName = userName;
45 }
46
47 public String getEmail() {
48 return email;
49 }
50
51 public void setEmail(String email) {
52 this.email = email;
53 }
54
55 public Integer getAge() {
56 return age;
57 }
58
59 public void setAge(Integer age) {
60 this.age = age;
61 }
62
63 public Integer getSalary() {
64 return salary;
65 }
66
67 public void setSalary(Integer salary) {
68 this.salary = salary;
69 }
70
71 public Date getBirthday() {
72 return birthday;
73 }
74
75 public void setBirthday(Date birthday) {
76 this.birthday = birthday;
77 }
78
79 }
搞一個控制器,開始對欄位進行校驗判斷,如下所示:
1 package com.demo.controller;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Iterator;
5
6 import javax.validation.Valid;
7
8 import org.springframework.beans.propertyeditors.CustomDateEditor;
9 import org.springframework.stereotype.Controller;
10 import org.springframework.validation.BindingResult;
11 import org.springframework.validation.ObjectError;
12 import org.springframework.web.bind.WebDataBinder;
13 import org.springframework.web.bind.annotation.InitBinder;
14 import org.springframework.web.bind.annotation.PostMapping;
15 import org.springframework.web.bind.annotation.ResponseBody;
16
17 import com.demo.po.UserInfo;
18
19 @Controller
20 public class ValidationController {
21
22 /**
23 *
24 *
25 * @param userInfo
26 * @param result
27 * @return
28 */
29 @PostMapping(value = "member_add")
30 @ResponseBody
31 public Object add(@Valid UserInfo userInfo, BindingResult result) {
32 // 執行的驗證出現錯誤
33 if (result.hasErrors()) {
34 Iterator<ObjectError> iterator = result.getAllErrors().iterator();
35 // 獲取全部的錯誤
36 while (iterator.hasNext()) {
37 // 取出每一個錯誤
38 ObjectError error = iterator.next();
39 System.out.println("【錯誤資訊】 code = " + error.getCode() + ", message = " + error.getDefaultMessage());
40 }
41 return result.getAllErrors();
42 } else {
43 return userInfo;
44 }
45 }
46
47 /**
48 * 本程式需要對日期格式進行處理
49 *
50 * @param binder
51 */
52 @InitBinder
53 public void initBinder(WebDataBinder binder) {
54 // 建立一個可以將字串轉換為日期的程式類
55 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
56 // 明確的描述此時需要注冊一個日期格式的轉換處理程式
57 binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, true));
58 }
59
60 }
然后搞個postman測驗一下自己的校驗介面是否正常,如下所示:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/222828.html
標籤:java
下一篇:Tomcat原始碼學習記錄
