spring 项目中使用 hibernate validator验证输入参数
程序员文章站
2022-07-01 23:34:03
spring 项目中使用 hibernate validator验证输入参数 ......
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有以下两种验证模式:
普通模式(会校验完所有的属性,然后返回所有的验证失败信息)
快速失败返回模式(只要有一个验证失败,则返回)
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