springboot 验证 默认使用的是hibernate validator ,不用额外增加引用包,springboot已经内置包含。
设置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.itstudy</groupId> <artifactId>demo</artifactId> <version>1.0.0-SNAPSHOT</version> <name>demo</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.30</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
1)定义接收参数实体
package com.study.valid_demo.vo; import javax.validation.constraints.AssertFalse; import javax.validation.constraints.Pattern; import org.hibernate.validator.constraints.NotBlank; /** * 接收客户端信息载体 * * @author Administrator * */ public class RegisterVO { @NotBlank(message = "用户名不能为空") private String userName; @NotBlank(message = "年龄不能为空") @Pattern(regexp = "^[0-9]{1,2}$", message = "年龄不正确") private String age; @AssertFalse(message = "必须为false") private Boolean isFalse; /** * 如果是空,则不校验,如果不为空,则校验 */ @Pattern(regexp = "^[0-9]{4}-[0-9]{2}-[0-9]{2}$", message = "出生日期格式不正确") private String birthday; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Boolean getIsFalse() { return isFalse; } public void setIsFalse(Boolean isFalse) { this.isFalse = isFalse; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } }
2.定义controller
package com.study.valid_demo; import javax.validation.Valid; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.study.valid_demo.vo.RegisterVO; @RestController @RequestMapping("/index") public class IndexController { @RequestMapping("/register") @ResponseBody public String register(@RequestBody @Valid RegisterVO register, BindingResult result) { // 使用BindingResult 时,验证错误需要自己处理。应用程序程序正常返回 if (result.hasErrors()) { for (ObjectError error : result.getAllErrors()) { System.out.println(error.getDefaultMessage()); } } return register.getUserName(); } @RequestMapping("/register2") @ResponseBody public String register(@RequestBody @Valid RegisterVO register) { // 不使用ResultBind时,在spring验证绑定时报400 Badrequest错误,并提示错误详细信息 return register.getUserName(); } }
3)项目启动
import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication app = new SpringApplication(App.class); //关闭banner app.setBannerMode(Banner.Mode.OFF); app.run(args); } }