springboot分组校验

1、分组校验场景

        主要2个场景,场景1:多个接口使用相同的入参,不同接口需要校验的内容不同。场景2:针对同一个接口,某个值(一般是类型)的不同会影响其他值的内容,此时需要根据某个值的内容来做不通的校验。

场景1(不同接口,相同入参,不同接口校验不同的内容)

入参类:

@Data
public class StudentReq {
    @NotNull(message = "id不能为空", groups = StudentValidator.Update.class)
    private String id;

    @NotNull(message = "性别不能为空", groups = StudentValidator.Insert.class)
    private String sex;

    private String project;
}

校验标识类

public class StudentValidator {
    public interface Insert{
    }

    public interface Update{
    }
}

2个接口

    /**
     * 通过接口实现不同的校验(接口维度)
     */
    @PostMapping("/test/validate/insert")
    public String insert(@RequestBody @Validated(StudentValidator.Insert.class) StudentReq studentReq) {
        return "insert";
    }

     /**
      * 通过接口实现不同的校验(接口维度)
      */
    @PostMapping("/test/validate/update")
    public String update(@RequestBody @Validated(StudentValidator.Update.class) StudentReq studentReq) {
        return "update";
    }

接口insert和update的入参都是StudentReq,其中的字段id和sex分别被注解StudentValidator.Update.class和StudentValidator.Insert.class表示,同时接口层面也使用了注解标识。则接口会校验被标识的字段。

运行结果

insert接口:

 update接口

场景2(入参的某值不同导致校验其他值的逻辑变化)

入参:

@Data
@GroupSequenceProvider(StudentWorkValidateProvider.class)
public class StudentWorkReq {
    @NotNull(message = "数学竞赛证书不能为空", groups = StudentWorkValidator.Math.class)
    private String mathCertificate;

    @NotNull(message = "计算机证书不能为空", groups = StudentWorkValidator.Computer.class)
    private String computerCertificate;

    private String project;
}

校验标识类

public class StudentWorkValidator {
    public interface Math{
    }

    public interface Computer{
    }
}

校验序列类:如果参数studentWorkReq的值是math时,则mathCertification不能为空。如果取值是computer,则computerCertification不能为空

public class StudentWorkValidateProvider implements DefaultGroupSequenceProvider<StudentWorkReq> {
    @Override
    public List<Class<?>> getValidationGroups(StudentWorkReq studentWorkReq) {
        List<Class<?>> defaultGroupSequence = new ArrayList<>();
        defaultGroupSequence.add(StudentWorkReq.class);
        if (Objects.isNull(studentWorkReq)) {
            return defaultGroupSequence;
        }

        if (studentWorkReq.getProject().equals("math")) {
            defaultGroupSequence.add(StudentWorkValidator.Math.class);
        } else if (studentWorkReq.getProject().equals("computer")) {
            defaultGroupSequence.add(StudentWorkValidator.Computer.class);
        }
        return defaultGroupSequence;
    }
}

接口

    /**
     * 通过接口参数实现不同的校验(参数维度)
     */
    @PostMapping("/work/validate/update")
    public String work(@RequestBody @Validated StudentWorkReq studentWorkReq) {
        return "work";
    }

运行结果

project传值math,mathCertification为空,则校验如下

 project传值computer,computerCertification为空,则校验如下

3、小结

2种场景,接口维度做不通校验和按照某个入参值(比如类型、类别能具有可列举属性的值)维度做不通校验

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值