<dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency>
注意:
(1)如果您已经使用了 spring-boot-starter-web,那它已经引入了最合适的 validation-api 版本 ,不用单独引依赖。如下图:
(2)低版本的 validation-api 的空校验只有 @NotNull ,高版本的请注意区分 @NotNull、@NotEmpty 和 @NotBlank 的使用。
完整的 pom.xml 内容如下:
<?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.huangxin</groupId> <artifactId>demo_springboot_validation</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <name>demo_springboot_validation</name> <description>Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
下面是 Spring Boot 的启动类,代码如下:
package com.hxstrive.springboot.validation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author hxstrive.com * @since 1.0.0 2024/2/19 9:04 */ @SpringBootApplication public class AppMain { public static void main(String[] args) { SpringApplication.run(AppMain.class, args); } }
CommonResult 类
package com.hxstrive.springboot.validation.dto; /** * 通用结果对象 * @author hxstrive.com * @since 1.0.0 2024/2/2 9:17 */ public class CommonResult<T> { private boolean state; private String message; private T data; public static <T> CommonResult<T> ok(T data) { CommonResult<T> result = new CommonResult<>(); result.setState(true); result.setData(data); return result; } public boolean isState() { return state; } public void setState(boolean state) { this.state = state; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
下面通过在类上面添加 @Validated 注解来实现简单参数验证,如下:
package com.hxstrive.springboot.validation.controller; import com.hx.springboot.validation.dto.CommonResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; /** * 在类上加 org.springframework.validation.annotation.Validated 注解,这里必须是在类上加,在方法或者参数上加,验证会不起作用 * @author hxstrive.com * @since 1.0.0 2024/2/19 9:06 */ @RestController @RequestMapping("/simple") @Validated public class SimpleArgDemoController { /** * 根据 id 和 name 查询数据 */ @GetMapping("/getData") @ResponseBody public CommonResult<String> getData( @Size(min = 4, max = 10,message = "id 长度应为4~10") @NotNull(message = "id 不能为空") String id, @Size(min = 2, max = 64,message = "name 长度应为2~64") @NotNull(message = "name 不能为空") String name) { return CommonResult.ok("id=" + id + ", name=" + name); } }
启动项目,使用 ApiFox 调用接口,如下图:
复杂参数是指接口的参数不是一个原始 Java 类型,而是一个 Java 自定义对象,例如:下面定义了一个请求参数对象:
package com.hxstrive.springboot.validation.dto; import lombok.Data; import lombok.ToString; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; /** * 参数对象 * @author hxstrive.com * @since 1.0.0 2024/2/19 9:20 */ @Data @ToString public class GetDataRequest { @Size(min = 4, max = 10,message = "id 长度应为4~10") @NotNull(message = "id 不能为空") private String id; @Size(min = 2, max = 64,message = "name 长度应为2~64") @NotNull(message = "name 不能为空") private String name; }
创建一个 controller,在方法的参数列表中使用 @Validated 注解验证参数,例如:
package com.hxstrive.springboot.validation.controller; import com.hx.springboot.validation.dto.CommonResult; import com.hx.springboot.validation.dto.GetDataRequest; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 复杂参数验证,需要在参数前面加 @Validated 注解 * @author hxstrive.com * @since 1.0.0 2024/2/19 9:17 */ @RestController @RequestMapping("/complex") public class ComplexArgDemoController { /** * 根据 id 和 name 查询数据 */ @GetMapping("/getData") public CommonResult<String> getData(@RequestBody @Validated GetDataRequest arg) { return CommonResult.ok(arg.toString()); } }
运行应用,使用 ApiFox 访问接口,如下图: