1 hibernate validator 官方文档:https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/
在 springboot 项目中 spring-boot-starter-web 已经包含了 hibernate-validator 可以直接使用。否则需要在 maven 依赖中添加依赖项。
<dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator-modules</artifactId> <version>6.0.13.Final</version> </dependency>
2 对请求的参数进行校验
import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import javax.validation.Valid; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import java.util.List; @Data @AllArgsConstructor @NoArgsConstructor @Builder public class RequestParamBase { @NotBlank(message = "name 不能为空") private String name; @NotEmpty(message = "fruits 不能为空") @Valid private List<Fruit> fruits; } import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; @AllArgsConstructor @NoArgsConstructor @Data public class Fruit { @NotBlank(message = "color 不能为空") private String color; @NotNull(message = "price 不能为空") private Integer price; }
说明:对于对象级联验证,需要在对象字段上添加 @Valid 标记。
3 在 controller 方法上使用验证
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.RequestMethod;import org.springframework.web.bind.annotation.RestController;import javax.validation.Valid; @RequestMapping(value = "/student/1", method = RequestMethod.POST) public Response getStudent(@RequestBody @Valid RequestParamBase paramBase, BindingResult validResult) { if (validResult.hasErrors()) { for(ObjectError error : validResult.getAllErrors()) { System.out.println(error.getDefaultMessage()); } return null; } System.out.println(paramBase); Response result = Response.builder().age(23).name("well").build(); return result; }
此时会发现会对所有字段验证,如果想验证出第一条问题赶快退出,就需要添加配置。
通常按顺序验证到第一个字段不符合验证要求时,就可以直接拒绝请求了。Hibernate Validator有以下两种验证模式:
1、普通模式(默认是这个模式)
普通模式(会校验完所有的属性,然后返回所有的验证失败信息)
2、快速失败返回模式
快速失败返回模式(只要有一个验证失败,则返回)
4 配置快速失败
failFast:true 快速失败返回模式 false 普通模式
ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class ) .configure() .failFast( true ) .buildValidatorFactory();Validator validator = validatorFactory.getValidator();
和 (hibernate.validator.fail_fast:true 快速失败返回模式 false 普通模式)
ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class ) .configure() .addProperty( "hibernate.validator.fail_fast", "true" ) .buildValidatorFactory();Validator validator = validatorFactory.getValidator();
在 springboot 项目中配置
import org.hibernate.validator.HibernateValidator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; @Configuration public class ValidConfig { @Bean public Validator validator() { ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class) .configure() .addProperty("hibernate.validator.fail_fast", "true") .buildValidatorFactory(); Validator validator = validatorFactory.getValidator(); return validator; } }
5 如果在 controller 中有多个入参,可以按照以下方式进行配置
public void test()(@RequestBody @Valid DemoModel demo, BindingResult result,@RequestBody @Valid DemoModel demo2, BindingResult result2)
参考文献:https://www.cnblogs.com/mr-yang-localhost/p/7812038.html
原文地址:https://www.cnblogs.com/zhaopengcheng/p/9963996.html
时间: 2024-10-27 17:33:55