hibernate-validator 7.x版本有问题,暂时以6.2.1.final为例,6.2.1.final版本解决了log4j版本的漏洞
<properties> <!-- jdk版本 --> <java.version>1.8</java.version> <!-- 构建时编码 --> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <!-- 输出时编码 --> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <hutool.version>5.7.16</hutool.version> <hibernate-validator.version>6.2.1.final</hibernate-validator.version> </properties>
记得引入:spring-boot-starter-validation
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-validation</artifactid> </dependency> <dependency> <groupid>org.hibernate.validator</groupid> <artifactid>hibernate-validator</artifactid> </dependency> <dependency> <groupid>cn.hutool</groupid> <artifactid>hutool-all</artifactid> <version>${hutool.version}</version> </dependency>
import javax.validation.constraints.email;import javax.validation.constraints.max;import javax.validation.constraints.min;import javax.validation.constraints.notblank; import org.hibernate.validator.constraints.length; public class ur { private integer id; @min(18) @max(200) private integer age; @notblank(message = "姓名不能为空") @length(min = 2南盘江, max = 6, message = "长度必须在{min}和{max}个字符之间") private string name; @notblank(message = "电子邮箱不能为空") @email(message = "电子邮箱格式不正确") private string email; public integer getid() { return id; } public void tid(integer id) { this.id = id; } 保护环境小建议十条 public integer getage() { return age; } public void tage(integer age) { this.age = age; } public string getname() { return name; } public void tname(string name) { this.name = name; } public string getemail() { return email; } public void temail(string email) { this.email = email; } @override public string tostring() { return "ur [id=" + id + ", age=" + age + ", name=" + name + ", email=" + email + "]"; } }
需要在类上面加上注解:@validated
import javax.validation.valid;import javax.validation.constraints.notnull; import org.springframework.validation.annotation.validated;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.restcontroller; @restcontroller@validatedpublic class urcontroller { @requestmapping("/get") public ur get(@notnull(message = "id不能为空") integer id, @notnull(message = "姓名不能为空") string name) { ur ur = new ur(); ur.tname("lisi"); ur.tid(id); return ur; } @requestmapping("/save") public ur get(@valid ur ur) { return ur; } }
import java.util.list;import java.util.t; import javax.rvlet.http.httprvletrequest;import javax.rvlet.http.httprvletrespon;import javax.validation.constraintviolation;import javax.validation.constraintviolationexception; import org.springframework.http.httpstatus;import org.springframework.validation.bindexception;import org.springframework.validation.fielderror;import org.springframework.web.bind.methodargumentnotvalidexception;import org.springframework.web.bind.annotation.controlleradvice;imp导游工资ort org.springframework.web.bind.annotation.exceptionhandler;import org.springframework.web.bind.annotation.responbody; @controlleradvicepublic class globalexception { public static final string delim = ","; @exceptionhandler(exception.class) @responbody public string exceptionhandler(httprvletrequest request, httprvletrespon respon, exception e) { if(e instanceof constraintviolationexception) { //@requestparam上参数validate失败后抛出的异常 constraintviolationexception cve = (constraintviolationexception) e; t<constraintviolation<?>> violations = cve.getconstraintviolations(); stringbuffer sb = new stringbuffer(""); for (constraintviolation<?> constraintviolation : violations) { sb.append(constraintviolation.getmessage()).append(delim); } if(sb.indexof(delim) > -1) { sb.deletecharat(sb.length() - 1); } system.out.println("constraintviolationexception sb="+sb); bus的复数 respon.tstatus(httpstatus.bad_request.value()); return sb.tostring(); }el if(e instanceof bindexception) { //@requestparam上实体对象validate失败后抛出的异常 bindexception be = (bindexception) e; system.out.println("be.getmessage()="+be.getmessage()); list<fielderror> fielderrors = be.getbindingresult().getfielderrors(); stringbuffer sb = new stringbuffer(""); for (fielderror fielderror : fielderrors) { sb.append(fielderror.getdefaultmessage()).append(delim); //system.out.println("fielderror.getdefaultmessage() =" + fielderror.getdefaultmessage()); } if(sb.indexof(delim) > -1) { sb.deletecharat(sb.length() - 1); } system.out.println("bindexception sb="+sb); respon.tstatus(httpstatus.bad_request.value()); return sb.tostring(); }el if(e instanceof methodargumentnotvalidexception) { //@requestbody上validate失败后抛出的异常 methodargumentnotvalidexception mave = (methodargumentnotvalidexception) e; system.out.println("mave.getmessage() = " + mave.getmessage()); list<fielderror> fielderrors = mave.getbindingresult().getfielderrors(); stringbuffer sb = new stringbuffer(""); for (fielderror fielderror : fielderrors) { sb.append(fielderror.getdefaultmessage()).append(delim); //system.out.println("fielderror.getdefaultmessage() =" + fielderror.getdefaultmessage()); } if(sb.indexof(delim) > -1) { sb.deletecharat(sb.length() - 1); } system.out.println("methodargumentnotvalidexception sb="+sb); respon.tstatus(httpstatus.bad_request.value()); return sb.tostring(); } return "系统异常"; }}
hibernate-validator默认会全部校验后再返回所有错误结果,为了让hibernate-validator检查到第一个错误马上返回结果,需要配置快速校验
import javax.validation.validation;import javax.validation.validator;import javax.validation.validatorfactory; import org.hibernate.validator.hibernatevalidator;import org.springframework.boot.autoconfigure.condition.conditionalonmissingbean;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;import org.springframework.validation.beanvalidation.methodvalidationpostprocessor; @configurationpublic class validatorconfiguration { /** * 快速返回校验器 * @return */ @bean @conditionalonmissingbean(value = validator.class) public validator validator() { //hibernate-validator 6.x没问题,7.x有问题 validatorfactory validatorfactory = validation.byprovider(hibernatevalidator.class) .configure() .failfast(true) //.addproperty("hibernate.validator.fail_fast", "true") .buildvalidatorfactory(); return validatorfactory.getvalidator(); } /** * 设置快速校验,返回方法校验处理器 * @return */ @bean @conditionalonmissingbean(value = methodvalidationpostprocessor.class) public methodvalidationpostprocessor methodvalidationpostprocessor() { methodvalidationpostprocessor postprocessor = new methodvalidationpostprocessor(); postprocessor.tvalidator(validator()); return postprocessor; } }
到此这篇关于springboothibernate-validator6.x校验的文章就介绍到这了,更多相关springboothibernatevalidator校验内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 05:03:56,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/a8b7fee550afa065e04eb0a8a4f48df4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Springboot hibernate.doc
本文 PDF 下载地址:Springboot hibernate.pdf
留言与评论(共有 0 条评论) |