SpringBoot使用validator校验
参考自 大老杨 的分享。
首先pom:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kf</groupId> <artifactId>validator-demo</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
建立需要校验的实体类:
package com.kf.entity; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; import javax.validation.constraints.Pattern; import java.io.Serializable; public class Demo implements Serializable { @NotEmpty(message="用户名不能为空") @Length(min=6,max = 12,message="用户名长度必须位于6到12之间") private String userName; @NotEmpty(message="密码不能为空") @Length(min=6,message="密码长度不能小于6位") private String passWord; @Email(message="请输入正确的邮箱") private String email; @Pattern(regexp = "^(\\d{18,18}|\\d{15,15}|(\\d{17,17}[x|X]))$", message = "身份证格式错误") private String idCard; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } }
编写controller
package com.kf.entity.com.kf.controller; import com.kf.entity.Demo; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.Mapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct; import javax.validation.Valid; import java.util.List; @RestController public class TestDemoController { @PostMapping("/") public String testDemo(@Valid Demo demo, BindingResult bindingResult){ StringBuffer stringBuffer = new StringBuffer(); if(bindingResult.hasErrors()){ List<ObjectError> list =bindingResult.getAllErrors(); for (ObjectError objectError:list) { stringBuffer.append(objectError.getDefaultMessage()); stringBuffer.append("---"); } } return stringBuffer!=null?stringBuffer.toString():""; } }
编写启动类后启动,
由于控制层用的POST,所以用的是Postman进行测试:
基本功能完成,更多内容参考: