当使用apifox时,我们需要必传字段做标记,可以使用
@NotEmpty(message = "id不能为空")
同时在入参位置添加 @Valid @RequestBody
其中
@Valid起到关键作用
效果图
同时在apifox中
这样测试或者前端去测试接口的时候就知道哪些字段一定要传,哪些是非必要的
@NotEmpty引入jar包import javax.validation.constraints.NotEmpty;
springboot项目可以直接使用
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId> </dependency>
在这里两个注解使用哪个注解都是一样的
下面对@Validated的功能进行进一步说明
比如添加操作和更新操作,使用同一个类,类中同一字段进行不同的说明
@NotBlank(groups = OnCreate.class, message = "文档ID不能为空且不能是空白")@Size(min = 5, groups = OnUpdate.class)private String documentId;
效果图
完整代码
<?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.java</groupId><artifactId>valid-param</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.14</version><relativePath/></parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!--tomcat容器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--lombok依赖--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency></dependencies></project>
代码部分
package com.java;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class Main {public static void main(String[] args) {SpringApplication.run(Main.class, args);} }package com.java;import lombok.Data;import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size;/*** @Description:* @Author: 邱燮* @Create: 2024/9/30 10:40*/ @Data public class BizDocReq {/*** 合同文本编号*/@NotEmpty(message = "合同文本编号不能为空")private String bizDocNo;@NotBlank(groups = OnCreate.class, message = "文档ID不能为空且不能是空白")@Size(min = 5, groups = OnUpdate.class)private String documentId;}package com.java;import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler;import java.util.HashMap; import java.util.Map;/*** @Description:* @Author: 邱燮* @Create: 2024/9/30 11:02*/ @ControllerAdvice public class GlobalExceptionHandler {@ExceptionHandler(MethodArgumentNotValidException.class)public ResponseEntity<Object> handleValidationExceptions(MethodArgumentNotValidException ex) {Map<String, Object> response = new HashMap<>();StringBuilder errors = new StringBuilder();ex.getBindingResult().getAllErrors().forEach((error) -> {String errorMessage = error.getDefaultMessage();errors.append(errorMessage).append("; ");});response.put("code", 400);response.put("data", null);response.put("msg", "请求参数不正确: " + errors.toString().trim());return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);} }public interface OnCreate { }public interface OnUpdate { }
接口层
package com.java;import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*;/*** @Description:* @Author: 邱燮* @Create: 2024/9/30 10:43*/ @RestController public class OperationDataController {@PostMapping("/queryContract")public String queryContract(@Validated @RequestBody BizDocReq bizDocReq) {return "success";}@PostMapping("/documents")public ResponseEntity<String> createDocument(@Validated(OnCreate.class) @RequestBody BizDocReq bizDocReq) {return ResponseEntity.ok("Document created successfully!");}@PutMapping("/updateDocument")public ResponseEntity<String> updateDocument(@Validated(OnUpdate.class) @RequestBody BizDocReq bizDocReq) {return ResponseEntity.ok("Document updated successfully!");}}