spring-亚博电竞手机版
目录
t; import org.springframework.validation.bindexception; import org.springframework.validation.bindingresult; import org.springframework.validation.fielderror; import org.springframework.web.bind.methodargumentnotvalidexception; import org.springframework.web.bind.annotation.exceptionhandler; import org.springframework.web.bind.annotation.restcontrolleradvice; import lombok.extern.slf4j.slf4j; @slf4j @restcontrolleradvice public class globalexceptionhandler { @exceptionhandler(value = bindexception.class) public jsonresult exceptionhandle(bindexception exception) { bindingresult result = exception.getbindingresult(); stringbuilder errormsg = new stringbuilder(); list另外jsonresult.java用于接口返回统一个json格式
import com.fasterxml.jackson.annotation.jsoninclude; import lombok.allargsconstructor; import lombok.data; import lombok.noargsconstructor; import lombok.experimental.accessors; /** *@author wang *@date 2020-9-14 * **/ @data @accessors(chain = true) @noargsconstructor @allargsconstructor @jsoninclude(jsoninclude.include.non_null) public class jsonresult
当然还有其他异常处理方式可以参考:https://www.jb51.net/article/244379.htm
七、测试效果
八、嵌套对象的校验
import lombok.data; import javax.validation.valid; import javax.validation.constraints.*; @data public class user { @notempty(message = "用户名不能为空") @size(message = "长度 [1-3] ", min = 1, max = 3, groups = validategroup.firstgroup.class) private string username; @min(message = "id不得小于0", value = 0) private integer id; @notblank(message = "地址不能为空", groups = {validategroup.thirdgroup.class, validategroup.secondegroup.class}) private string address; //在内部属性是自定义对象的时候添加 @valid 注解 ,即可开启对in对象的校验。 //记得添加@notnull注解,否则该对象可以为null,并且此时in对象的校验规则也不会抛出异常 。 @valid @notnull(message = "in 对象不能为null ", groups = {validategroup.thirdgroup.class, validategroup.secondegroup.class}) private in in; }
/** *自定义的对象,user对象中的一个属性。 */ @data public class in { @notblank(message = "str不能为空", groups = {validategroup.thirdgroup.class, validategroup.secondegroup.class}) private string str; }
九、自定义注解(自定义校验规则)
9.1 实现constraintvalidator接口
import javax.validation.constraintvalidator; import javax.validation.constraintvalidatorcontext; public class wordconstraintvalidator implements constraintvalidator
9.2 自定义注解
import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; import javax.validation.constraint; import javax.validation.payload; @target({ elementtype.method, elementtype.field }) @retention(retentionpolicy.runtime) @constraint(validatedby = wordconstraintvalidator.class) public @interface customvalidaor { string message(); // groups 和 payload 这两个parameter 必须包含,不然会报错 class[] groups() default {}; class[] payload() default {}; }
9.3 使用
@data public class inner { @notblank(message = "str不能为空", groups = { validategroup.thirdgroup.class, validategroup.secondegroup.class }) @customvalidaor(message = "长度必须为10", groups = { validategroup.thirdgroup.class, validategroup.secondegroup.class }) private string str; }
9.4测试
访问接口
@postmapping("test6") public user test6(@validated({validategroup.secondegroup.class}) @requestbody user u) { system.out.println(u); return u; }
到此这篇关于spring-boot-starter-validation 校验参数的实现的文章就介绍到这了,更多相关spring-boot-starter-validation 校验参数内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!