spring-mile米乐体育

目录

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 fielderrors = result.getfielderrors(); fielderrors.foreach(error -> { log.error("field: " error.getfield() ", msg:" error.getdefaultmessage()); errormsg.append(error.getdefaultmessage()).append("!"); }); return jsonresult.fail(errormsg.tostring()); } @exceptionhandler(value = methodargumentnotvalidexception.class) public jsonresult myexceptionhandle(methodargumentnotvalidexception exception) { bindingresult result = exception.getbindingresult(); stringbuilder errormsg = new stringbuilder(); list fielderrors = result.getfielderrors(); fielderrors.foreach(error -> { log.error("field: " error.getfield() ", msg:" error.getdefaultmessage()); errormsg.append(error.getdefaultmessage()).append("!"); }); return jsonresult.fail(errormsg.tostring()); } // 处理运行时异常 @exceptionhandler(runtimeexception.class) public jsonresult dohandleruntimeexception(runtimeexception e) { log.error(e.getmessage(), e); e.printstacktrace(); return jsonresult.fail(e.getmessage()); } }

另外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 { /** 成功 */ public static final int success=200; /**内部服务器错误**/ public static final int fail=500; /** 没有登录 */ public static final int not_login = 400; /** 发生异常 */ public static final int exception = 401; /** 系统错误 */ public static final int sys_error = 402; /** 参数错误 */ public static final int params_error = 403; /** 不支持或已经废弃 */ public static final int not_supported = 410; /** authcode错误 */ public static final int invalid_authcode = 444; /** 太频繁的调用 */ public static final int too_frequent = 445; /** 未知的错误 */ public static final int unknown_error = 499; private integer code; private string msg; private t data; public static jsonresult fail() { return new jsonresult(fail, "请求处理失败",null); } public static jsonresult fail(string msg) { return new jsonresult(fail, msg,null); } public static jsonresult fail(integer code,string msg) { return new jsonresult(code, msg,null); } public static jsonresult success() { return new jsonresult(success,"请求处理成功",null); } public static jsonresult success(string msg) { return new jsonresult(success,msg,null); } public static jsonresult success(t data) { return new jsonresult (success,"请求处理成功",data); } public static jsonresult success(string msg,t data) { return new jsonresult(success, msg,data); } public static jsonresult err() { return build(exception); } public static jsonresult err(string msg) { return build(exception, msg); } public jsonresult code(int code) { this.code = code; return this; } public jsonresult msg(string msg) { this.msg = msg; return this; } public jsonresult data(t data) { this.data = data; return this; } public static jsonresult build() { return new jsonresult(); } public static jsonresult build(int code) { return new jsonhttp://www.cppcns.comresult().code(code); } public static jsonresult build(int code, string msg) { return new jsonresult().code(code).msg(msg); } public static jsonresult build(int code, t data) { return new jsonresult().code(code).data(data); } public static jsonresult build(int code, string msg, t data) { return new jsonresult().code(code).msg(msg).data(data); } }

当然还有其他异常处理方式可以参考: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 { @override public boolean isvalid(object value, constraintvalidatorcontext context) { // 具体的校验规则 return value.tostring().length() == 10; } }

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 校验参数内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

展开全文
内容来源于互联网和用户投稿,文章中一旦含有米乐app官网登录的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系米乐app官网登录删除

最新文章

网站地图