时间:2021-05-02
spring boot开发web项目有时候我们需要对controller层传过来的参数进行一些基本的校验,比如非空、整数值的范围、字符串的长度、日期、邮箱等等。spring支持jsr-303 bean validation api,可以方便的进行校验。
使用注解进行校验
先定义一个form的封装对象
? 1 2 3 4 5 6 7 8 9 10 class requestform { @size(min = 1, max = 5) private string name; public string getname() { return name; } public void setname(string name) { this.name = name; } }其中name这个字段用size注解限制长度1到5。size是javax.validation包中的constraint注解。
在使用时用@valid注解表示要校验这个bean。
? 1 2 3 4 5 6 @responsebody @getmapping(value = "bean") public string validate(@valid requestform request) { system.out.println(request.getname()); return "ok"; }自定义注解
如果内置的注解不够用,可以自定义注解。
比如先定义一个注解nameconstraint,限制name字段只能从特定数据中选取。
? 1 2 3 4 5 6 7 8 9 @target({ elementtype.field, elementtype.parameter }) @retention(retentionpolicy.runtime) @constraint(validatedby = nameconstraintvalidator.class) @interface nameconstraint { string[] allowedvalues(); class<?>[] groups() default {}; class<? extends payload>[] payload() default {}; string message(); }其中allowedvalues表示合法的取值范围,message是校验失败的显示信息。
message、groups、payload是hibernate validator要求的字段,想了解的请看官方文档
再定义一个validator做真正的校验
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class nameconstraintvalidator implements constraintvalidator<nameconstraint, string> { private string[] validvalues; @override public void initialize(nameconstraint constraintannotation) { validvalues = constraintannotation.allowedvalues(); } @override public boolean isvalid(string value, constraintvalidatorcontext context) { for (string s : this.validvalues) { if (s.equals(value)) { return true; } } return false; } }在form bean中按如下方式使用
? 1 2 3 4 5 6 7 8 9 10 class requestformwithcustomconstraint { @nameconstraint(allowedvalues = { "bar", "foo" }, message = "只允许bar,foo") private string name; public string getname() { return name; } public void setname(string name) { this.name = name; } }直接校验参数
只有一个name字段,不想封装一个对象怎么办?可以直接校验该参数
? 1 2 3 4 5 6 7 8 9 10 11 @controller @validated @requestmapping(value = "validator") public class parametervalidatordemocontroller { @responsebody @getmapping(value = "simple") public string validateparameter(@size(min = 1, max = 5) string name) { system.out.println(name); return "ok"; } }controller上面的@validated注解则告诉spring需要扫描这个类,来检查其中的constraint注解。
详细信息可以参考官方文档有关章节
https://docs.spring.io/spring-boot/docs/1.5.9.release/reference/htmlsingle/#boot-features-validation
https://docs.spring.io/spring/docs/4.3.16.release/spring-framework-reference/htmlsingle/#validation-beanvalidation
代码在github
https://github.com/kabike/spring-boot-demo
总结
以上所述是小编给大家介绍的spring boot进行参数校验的方法实例详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
原文链接:https://www.jianshu.com/p/88610959cbc7
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
项目升级到springboot2.3之后,参数校验的注解报错,发现spring-boot-starter-web的依赖项已经去除了依赖点开spring-boot
上文我们讨论了spring-boot如何去获取前端传递过来的参数,那传递过来总不能直接使用,需要对这些参数进行校验,符合程序的要求才会进行下一步的处理,所以本篇
本文介绍了springboot的maven配置依赖详解,分享给大家,具体如下:我们通过引用spring-boot-starter-parent,添加spring
1.什么是spring-boot-devtoolsspring-boot-devtools是spring-boot项目开发时的一个热部署工具,安装了spring
一)spring-boot-starter命名规则自动配置模块命名规则:xxx-spring-boot,如:aspectlog-spring-boot启动器命名