`

注解级联校验 对象中的list

阅读更多

级联校验的关键 在list上加 @Valid 在需要校验的list对象加 @Validated

 

——————————————————————————————————————

@Validated和@Valid的区别:

 

1、@Valid:标准JSR-303规范的标记型注解,用来标记验证属性和方法返回值,进行级联和递归校验

2、@Validated:Spring的注解,是标准JSR-303的一个变种(补充),提供了一个分组功能,可以在入参验证时,根据不同的分组采用不同的验证机制

3、在Controller中校验方法参数时,使用@Valid和@Validated并无特殊差异(若不需要分组校验的话)

4、@Validated注解可以用于类级别,用于支持Spring进行方法级别的参数校验。@Valid可以用在属性级别约束,用来表示级联校验。

 

5、@Validated只能用在类、方法和参数上,而@Valid可用于方法、字段、构造器和参数上

————————————————————————————————————————

直接上代码:

 

@RestController

@RequestMapping("/sys/user")

@Api(tags="用户")

public class UserController  {

    @Autowired

    private UserService userService;

 

    @PostMapping("/add")

    @ApiOperation("新增用户") //Swagger 文档注解

    public R add(@RequestBody @Valid AddUserForm addUserForm){

        try {

//业务逻辑处理

            userService.addOrUpdate(addUserForm);

        }catch (Exception e){

            e.printStackTrace();

        }

        return R.ok();

    }

 

}

 

 

@Data

@ApiModel

public class AddUserForm {

    

   

    @NotBlank(message = "请填写姓名")

    @Length(max = 30,min = 2,message = "姓名长度限制2~30字符")

    @ApiModelProperty(value = "姓名,required = true) //Swagger 文档注解

    private String userName;

 

 

    /**

     * 校验集合中的对象

     */

    @Valid  //级联效验的关键代码

    @NotNull(message ="请填写教育背景") //校验list不允许为空 必填项

    @ApiModelProperty(value = "教育背景",required = true)

    private List<AddUserEducationBackgroundForm> userEducationBackgroundFormList=new ArrayList<>(0);

 

}

 

@Validated //级联效验的关键代码

@Data

@ApiModel

public class AddUserEducationBackgroundForm {

 

    @NotBlank

    @Length(max = 30, min = 2,message = "学校长度限制2~30字符")

    @ApiModelProperty(value = "学校",required = true)

    private String school;

 

    @NotBlank

    @Length(max = 20, min = 2,message = "专业长度限制2~20字符")

    @ApiModelProperty(value = "专业",required = true)

    private String profession;

 

}

 

0
2
分享到:
评论

相关推荐

    Hibernate中文详细学习文档

    Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One shot delete) 19.6. 监测性能(Monitoring performance) 19.6.1. 监测SessionFactory 19.6.2. 数据记录(Metrics) 20. 工具箱指南 20.1. ...

    Hibernate+中文文档

    Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One shot delete) 19.6. 监测性能(Monitoring performance) 19.6.1. 监测SessionFactory 19.6.2. 数据记录(Metrics) 20. 工具箱指南 20.1. ...

    Hibernate 中文 html 帮助文档

    Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One shot delete) 19.6. 监测性能(Monitoring performance) 19.6.1. 监测SessionFactory 19.6.2. 数据记录(Metrics) 20. 工具箱指南 20.1. ...

    hibernate3.2中文文档(chm格式)

    Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One shot delete) 19.6. 监测性能(Monitoring performance) 19.6.1. 监测SessionFactory 19.6.2. 数据记录(Metrics) 20. 工具箱指南 20.1. ...

    Hibernate参考文档

    Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One shot delete) 19.6. 监测性能(Monitoring performance) 19.6.1. 监测SessionFactory 19.6.2. 数据记录(Metrics) 20. 工具箱指南 20.1. ...

    hibernate 体系结构与配置 参考文档(html)

    在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合标识符(Components as composite identifiers) 8.5. 动态组件 (Dynamic ...

    HibernateAPI中文版.chm

    Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One shot delete) 19.6. 监测性能(Monitoring performance) 19.6.1. 监测SessionFactory 19.6.2. 数据记录(Metrics) 20. 工具箱指南 20.1. ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One shot delete) 19.6. 监测性能(Monitoring performance) 19.6.1. 监测SessionFactory 19.6.2. 数据记录(Metrics) 20. 工具箱指南 20.1. ...

Global site tag (gtag.js) - Google Analytics