当前位置: 移动技术网 > IT编程>开发语言>Java > @Validated和@Valid校验参数、级联属性、List

@Validated和@Valid校验参数、级联属性、List

2019年10月21日  | 移动技术网IT编程  | 我要评论

@validated和@valid的区别

在controller中校验方法参数时,使用@valid和@validated并无特殊差异(若不需要分组校验的话):
@valid:标准jsr-303规范的标记型注解,用来标记验证属性和方法返回值,进行级联和递归校验
@validated:spring的注解,是标准jsr-303的一个变种(补充),提供了一个分组功能,可以在入参验证时,根据不同的分组采用不同的验证机制

方法级别:
@validated注解可以用于类级别,用于支持spring进行方法级别的参数校验。@valid可以用在属性级别约束,用来表示级联校验。
@validated只能用在类、方法和参数上,而@valid可用于方法、字段、构造器和参数上

校验参数、级联属性

1、校验参数
当入参为实体对象时,需要在方法上加@valid或@validated或者在参数前加@valid或@validated,或者在类上加@validated

@valid
@getmapping("/exam-info")
public boolean getinfo(user user){......}

@getmapping("/exam-info")
public boolean getinfo(@valid user user){......}

@validated
@getmapping("/exam-info")
public boolean getinfo(user user){......}

@getmapping("/exam-info")
public boolean getinfo(@validated user user){......}

public class user{
@notnull("id不能为空")
private integer id;
}

2、嵌套验证
     @valid作用于属性上有嵌套验证作用,@validated不能作用于属性上,如下代码在user类的属性car上添加@valid注解,当传参id为空时会报错。

@getmapping("/exam-info")
public boolean getinfo(@valid user user){.....}

public class user{
@valid
@notnull("car不能为空")
private car car;
}
public class car{
@notnull("id不能为空")
private integer id;
}

校验list

@valid只能校验javabean,而list不是javabean所以校验会失败,介绍三种解决办法

方法1:对list进行wrapper

既然list不是javabean,那我们就把它封装成javabean,我们定义一个listwrapper类如下:

import lombok.getter;
import lombok.setter;
import javax.validation.valid;
import java.util.arraylist;
import java.util.list;

@setter
@getter
public class listwrapper {
@valid
private list list;

public listwrapper() {
    list = new arraylist<>();
}

public  listwrapper(list<e> list) {
    this.list = list;
}

}
同时修改一下controller对应的方法:

// 使用包装类对list进行验证
@postmapping("/insert/all")
public serverresponse<string> insertlist(@valid @requestbody listwrapper<userentity> listwrapper, bindingresult bindingresult) {
    if(bindingresult.haserrors()) {
        log.error(bindingresult.getfielderror().tostring());
        return serverresponse.createbyerrormessage(bindingresult.getfielderror().getdefaultmessage());
    }

    userservice.insertlist(listwrapper.getlist());
    return serverresponse.createbysuccess();
}

这样就可以对list进行校验了

注意:

由于对list进行了包装,如果我们传参的时候

[{},{}..]要改为{“list”: [{},{}..]}

方法2:使用@validated+@valid

在controller类上面增加@validated注解,并且删除方法参数中的bindingresult bindingresult(因为这个参数已经没有用了,异常统一有controller返回了)
file
file

然后我们运行一下测试一下
file

可以看到可以对参数进行校验了,但还还有一个问题,那就是这个不是我们想要的返回格式,它controller自己返回的格式,所以我们需要做一个统一异常处理,代码如下:

import com.wyq.firstdemo.common.serverresponse;
import lombok.extern.slf4j.slf4j;
import org.springframework.web.bind.annotation.exceptionhandler;
import org.springframework.web.bind.annotation.responsebody;
import org.springframework.web.bind.annotation.restcontrolleradvice;

import javax.validation.constraintviolation;
import javax.validation.constraintviolationexception;
import java.util.set;

@slf4j
@restcontrolleradvice
public class controllerexceptionhandler {

@exceptionhandler
@responsebody
public serverresponse<string> handle(constraintviolationexception exception) {
    log.error(string.valueof(exception));
    set<constraintviolation<?>> violations = exception.getconstraintviolations();
    stringbuilder builder = new stringbuilder();
    for (constraintviolation violation : violations) {
        builder.append(violation.getmessage());
        break;
    }
    return serverresponse.createbyerrormessage(builder.tostring());
}

}
经过统一异常处理,我们这边的返回结果就是我们想要的格式了
file

方法3:自定义一个list

先上代码后说明,先定义一个validlist

import javax.validation.valid;
import java.util.*;

public class validlist implements list {

@valid
private list<e> list;

public validlist() {
    this.list = new arraylist<>();
}

public validlist(list<e> list) {
    this.list = list;
}

public list<e> getlist() {
    return list;
}

public void setlist(list<e> list) {
    this.list = list;
}

@override
public int size() {
    return list.size();
}

@override
public boolean isempty() {
    return list.isempty();
}

...

}
对比方法3和方法1,有没有觉得代码有点相似,新建一个类,并且让他实现list接口,使这个类即具有了javabean的特性,又具有了list的特性,比方法1简单优雅很多。

只需要把list换成validlist就可以了,还不需要多统一异常处理。
file

参考:https://my.oschina.net/fangshixiang/blog/3081366
https://stackoverflow.com/questions/28150405/validation-of-a-list-of-objects-in-spring/36313615#36313615
https://my.oschina.net/fangshixiang/blog/3081366

本文由博客一文多发平台 openwrite 发布!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网