zoukankan      html  css  js  c++  java
  • 分组校验功能(完成多场景的复杂校验)

    1、groups

    1、给校验注解,标注上groups,指定什么情况下才需要进行校验

    如:指定在更新和添加的时候,都需要进行校验。新增时不需要带id,修改时必须带id

    @NotNull(message = "修改必须定制品牌id", groups = {UpdateGroup.class})
    @Null(message = "新增不能指定id", groups = {AddGroup.class})
    @TableId
    private Long brandId;
    

      

    在这种情况下,没有指定分组的校验注解,默认是不起作用的。想要起作用就必须要加groups。

    2、@Validated

    2、业务方法参数上使用@Validated注解

    @Validated的value方法:

    Specify one or more validation groups to apply to the validation step kicked off by this annotation.
    指定一个或多个验证组以应用于此注释启动的验证步骤。

    JSR-303 defines validation groups as custom annotations which an application declares for the sole purpose of using
    them as type-safe group arguments, as implemented in SpringValidatorAdapter.

    JSR-303 将验证组定义为自定义注释,应用程序声明的唯一目的是将它们用作类型安全组参数,如 SpringValidatorAdapter 中实现的那样。

    Other SmartValidator implementations may support class arguments in other ways as well.

    其他SmartValidator 实现也可以以其他方式支持类参数。

    @RequestMapping("/save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand) {
        brandService.save(brand);
    
        return R.ok();
    }
    @RequestMapping("/delete")
    //@RequiresPermissions("${moduleNamez}:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));
    
        return R.ok();
    }
    

      

    /**
    	 * 品牌logo地址 修改可以不带上logoURL
    	 */
    	@NotBlank(groups = {AddGroup.class})
    	@URL(message = "logo必须是一个合法的URL地址", groups={AddGroup.class, UpdateGroup.class})
    	private String logo;
    注意上面因为@NotBlank没有指定UpdateGroup分组,所以不生效。此时update时可以不携带,但带了一定得是url地址
    

      

    3、分组情况下,校验注解生效问题

    3、默认情况下,在分组校验情况下,没有指定指定分组的校验注解,将不会生效,它只会在不分组的情况下生效。

     

  • 相关阅读:
    Centos7 系统更改apache默认网站目录(解决You don't have permission to access / on this server问题)
    1-18-2 LVM管理和ssm存储管理器使用&磁盘配额 (二)
    1-3 RHEL7操作系统的安装
    1-18-1 LVM管理和ssm存储管理器使用&磁盘配额(一)
    【转载】CentOS7下使用LVM给系统硬盘扩容
    自动备份Mysql数据库脚本
    docker安装nginx并配置通过https访问
    搭建本地yum源和局域网yum源
    【转载】使用Docker Hub官方gcc:latest镜像编译C/C++程序以及缩小镜像的方法
    【转载】如何使用docker部署c/c++程序
  • 原文地址:https://www.cnblogs.com/vincentmax/p/14415499.html
Copyright © 2011-2022 走看看