前言
在前后端分离开发的时候我们需要用到参数校验,前端需要进行参数校验,后端接口同样的也需要,以防传入不合法的数据。
1、首先还是先导包,导入pom文件。
org.springframework.boot
spring-boot-starter-validation
2、解释一下注解的作用
@null 限制只能为null
@notnull 限制必须不为null
@notempty 只作用于字符串类型,字符串不为空,并且长度不为0
@notblank 只作用于字符串类型,字符串不为空,并且trim()后不为空串
@assertfalse 限制必须为false
@asserttrue 限制必须为true
@decimalmax(value) 限制必须为一个不大于指定值的数字
@decimalmin(value) 限制必须为一个不小于指定值的数字
@digits(integer,fraction) 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位
数不能超过fraction
@future 限制必须是一个将来的日期
@past 验证注解的元素值(日期类型)比当前时间早
@max(value) 限制必须为一个不大于指定值的数字
@min(value) 限制必须为一个不小于指定值的数字
@pattern(value) 限制必须符合指定的正则表达式
@size(max,min) 限制字符长度必须在min到max之间
@email 验证注解的元素值是email,也可以通过正则表达式和flag指定自定义的email格式
3、在实体类加上要验证的字段。(我这里随便写下)
标注的地方就是用来分组校验的,下下面会解释。
@data
public class loginvo {
@apimodelproperty(value = "用户名称")
@notempty(message = "用户名不能为空!",groups = loginmodel.class)
@notempty(message = "添加时用户名不能为空!",groups = savemodel.class)
private string username;
@apimodelproperty(value = "密码")
@size(min = 2,message = "密码最少为2位",groups = loginmodel.class)
@size(min = 6,message = "密码最少为6位",groups = savemodel.class)
private string password;
}
通过groups的属性来分组,假设我在使用登录分组校验的时候,设定用户名不能为空和密码最少为2位的验证。而在添加分组设定添加时用户名不能为空和密码最少为6位的验证。
4、在来解释下上面标注的分组接口。
loginmodel
import javax.validation.groups.default;
public interface loginmodel extends default {
}
必须继承默认的defaut接口不然后抛出异常。
savemodel
import javax.validation.groups.default;
public interface savemodel extends default{
}
5、在controller的接口上加上@validated注解,参数就加上你需要根据那种规则来校验。
@apioperation(value = "登录以后返回token")
@postmapping(value = "/login")
public result login(@requestbody @validated(loginmodel.class) loginvo loginvo) {
string token = userservice.login(loginvo.getusername(), loginvo.getpassword());
return result.success(token);
}
运行后只能在控制台显示错误的结果,新的问题又来了怎么把错误的结果通过自己的result类返回给前端。这就需要对错误全局捕捉了。
6、写一个对response换回结果的处理。
@restcontrolleradvice
@slf4j
public class parametercalibration {
@exceptionhandler({methodargumentnotvalidexception.class, bindexception.class})
@responsestatus(httpstatus.ok)
@responsebody
public result handlemethodargumentnotvalidexception(exception exception) {
stringbuilder errorinfo = new stringbuilder();
bindingresult bindingresult=null;
if(exception instanceof methodargumentnotvalidexception){
bindingresult= ((methodargumentnotvalidexception)exception).getbindingresult();
}
if(exception instanceof bindexception){
bindingresult= ((bindexception)exception).getbindingresult();
}
for(int i = 0; i < bindingresult.getfielderrors().size(); i++){
if(i > 0){
errorinfo.append(",");
}
fielderror fielderror = bindingresult.getfielderrors().get(i);
errorinfo.append(fielderror.getfield()).append(" :").append(fielderror.getdefaultmessage());
}
log.error(errorinfo.tostring());
//这里返回自己的result的结果类。
return result.validatefailed(errorinfo.tostring());
}
@exceptionhandler(exception.class)
@responsestatus(httpstatus.bad_request)
@responsebody
public result handledefaultexception(exception exception) {
log.error(exception.tostring());
//这里返回自己的result的结果类。
return result.validatefailed("服务器错误",exception);
}
}
``
7. 先测试loginmodel的校验规则
控制台打印的数据
前端收到的数据
切换成savemodel控制台打印的数据
切换成savemodel前端收到的数据
可以看到两次的验证规则时不同的,完成了简易的分组操作。
8 、总结,就是在添加验证规则的时候指定对应的分组,在使用时传入需要的分组。可能理解有误,发现请指导。
总结
到此这篇关于springboot @validated注解实现参数分组校验的文章就介绍到这了,更多相关springboot @validated注解实现参数分组校验内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!
希望与广大网友互动??
点此进行留言吧!