亲宝软件园·资讯

展开

SpringBoot联合校验

fengyehongWorld 人气:0

参考资料

分组序列@GroupSequenceProvider、@GroupSequence控制数据校验顺序,解决多字段联合逻辑校验问题【享学Spring MVC】

Hibernate Validator提供了非标准的@GroupSequenceProvider注解。针对当前对象实例的状态,动态来决定加载那些校验组进入默认校验组。
需要借助Hibernate Validation提供给我们的DefaultGroupSequenceProvider接口来处理那些属性在什么情况下进入指定的分组。

一. 前期准备

⏹自定义校验数值不能为空的注解

@Documented
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {ValidateIntegerNotEmpty.StrictIntegerNotEmptyValidator.class})
@ReportAsSingleViolation
public @interface ValidateIntegerNotEmpty {

    String msgArgs() default "";

	String message() default "{1001E}";

	Class<?>[] groups() default {};

	Class<? extends Payload>[] payload() default {};

	class StrictIntegerNotEmptyValidator implements ConstraintValidator<ValidateIntegerNotEmpty, Integer> {

        @Override
        public boolean isValid(Integer value, ConstraintValidatorContext context) {

            return !ObjectUtils.isEmpty(value);
        }
    }
}

二. 需求

⏹前台

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <script type="text/javascript" th:src="@{/js/public/jquery-3.6.0.min.js}"></script>
    <script type="text/javascript" th:src="@{/js/common/common.js}"></script>
    <title>test7页面</title>
</head>
<body>

    <button id="btn">校验数据</button>

    <h1>我是test7的页面</h1>
</body>
<script>
    $("#btn").click(() => {

        const param1 = {
        	// 人工初审拒绝
            auditStatus: 2,
            // 拒绝的原因
            auditRejectReason: 5,
        };

        const url = `http://localhost:8080/test7/groupSequenceProvider`;
        doAjax(url, param1, function(data) {
            console.log(data);
        });
    });
</script>
</html>

⏹待校验的form1

import com.example.jmw.common.validation.ValidateIntegerNotEmpty;
import com.example.jmw.form.validation.ValidateTest7FormProvider;
import lombok.Data;
import org.hibernate.validator.constraints.Range;
import org.hibernate.validator.group.GroupSequenceProvider;

@Data
// 通过该注解所对应的自定义Provider来实现多属性联合校验
@GroupSequenceProvider(ValidateTest7FormProvider.class)
public class Test7Form {

    /**
     * 1: 审核中
     * 2: 人工初审拒绝
     * 3: 人工初审通过
     */
    @ValidateIntegerNotEmpty(msgArgs = "审核状态类型")
    @Range(min = 1, max = 3, message = "审核拒绝原因:参数传递错误")
    private Integer auditStatus;

    /**
     * 1: 不符合准入要求
     * 2: 三方数据拒贷
     * 3: 授信额度为0
     * 4: 其他
     */
    @ValidateIntegerNotEmpty(msgArgs = "审核拒绝原因", groups = auditGroup.class)
    @Range(min = 1, max = 4, message = "审核拒绝原因:参数传递错误", groups = auditGroup.class)
    private Integer auditRejectReason;
	
	// 自定义分组
    public interface auditGroup {
    }
}

⏹校验器

import com.example.jmw.form.Test7Form;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class ValidateTest7FormProvider implements DefaultGroupSequenceProvider<Test7Form> {

    @Override
    public List<Class<?>> getValidationGroups(Test7Form test7Form) {

        List<Class<?>> defaultGroupSequence = new ArrayList<>();
        defaultGroupSequence.add(Test7Form.class);

        if (ObjectUtils.isEmpty(test7Form)) {
            return defaultGroupSequence;
        }

        // 获取 人工初审 状态
        Integer auditStatus = Optional.ofNullable(test7Form.getAuditStatus()).orElse(0) ;

        // 如果 人工初审通过的话,审核拒绝原因的auditGroup组就会起作用,就变为必填项目,否则为选填项目
        if (auditStatus == 2) {
            defaultGroupSequence.add(Test7Form.auditGroup.class);
        }

        return defaultGroupSequence;
    }
}

⏹Controller层进行校验

@Controller
@RequestMapping("/test7")
public class Test7Controller {

    @Resource
    private LocalValidatorFactoryBean validator;

    @GetMapping("/init")
    public ModelAndView init() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("test7");
        return  modelAndView;
    }

    @PostMapping("/groupSequenceProvider")
    @ResponseBody
    public void groupSequenceProvider(@RequestBody Test7Form form) {

        Set<ConstraintViolation<Test7Form>> validate = validator.validate(form);
        for (ConstraintViolation<Test7Form> bean : validate) {

            // 获取当前的校验信息
            String message = bean.getMessage();
            System.out.println(message);
        }
    }
}

当参数auditStatus为2(人工初审拒绝)时,auditRejectReason(审核拒绝原因)超出了1到4的范围,因此显示出校验信息

当参数auditStatus为2(人工初审拒绝)时,auditRejectReason(审核拒绝原因)为必填项,因此显示出校验信息

当参数auditStatus为3(人工初审通过)时,auditRejectReason(审核拒绝原因)为非必填项,因此无校验失败信息

三. 需求

⏹前台

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <script type="text/javascript" th:src="@{/js/public/jquery-3.6.0.min.js}"></script>
    <script type="text/javascript" th:src="@{/js/common/common.js}"></script>
    <title>test7页面</title>
</head>
<body>

    <button id="btn">校验数据</button>

    <h1>我是test7的页面</h1>
</body>
<script>
    $("#btn").click(() => {

        const param2 = {
        	// 领导(2)访问
            role: 2,
            // 权限的数量为5
            permissionList: [1, 1, 1, 1, 1],
        };

        const url = `http://localhost:8080/test7/groupSequenceProvider`;
        doAjax(url, param2, function(data) {
            console.log(data);
        });
    });
</script>
</html>

⏹待校验的form2

import com.example.jmw.common.validation.ValidateIntegerNotEmpty;
import com.example.jmw.form.validation.ValidateTest7Form1Provider;
import lombok.Data;
import org.hibernate.validator.constraints.Range;
import org.hibernate.validator.group.GroupSequenceProvider;

import javax.validation.constraints.Size;
import java.util.List;

@Data
// 通过该注解所对应的自定义Provider来实现多属性联合校验
@GroupSequenceProvider(ValidateTest7Form1Provider.class)
public class Test7Form1 {

    /**
     * 1: 访客
     * 2: 领导
     * 3: 管理员
     */
    @ValidateIntegerNotEmpty(msgArgs = "角色类型")
    @Range(min = 1, max = 3, message = "错误原因:参数传递错误")
    private Integer role;

    @Size.List({
            // 访客1个权限
            @Size(min = 1, max = 2, message = "访客最多拥有2个权限", groups = GuestGroup.class),
            // 领导4个权限
            @Size(min = 1, max = 4, message = "领导最多拥有4个权限", groups = LeaderGroup.class),
            // 管理员10个权限
            @Size(min = 1, max = 10, message = "管理员最多拥有10个权限", groups = AdminGroup.class)
    })
    private List<Integer> permissionList;
    
    // 游客分组
    public interface GuestGroup {
    }
    
    // 领导分组
    public interface LeaderGroup {
    }
    
    // 管理员分组
    public interface AdminGroup {
    }
}

⏹校验器

import com.example.jmw.form.Test7Form1;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;
import org.springframework.util.ObjectUtils;

import java.util.*;

public class ValidateTest7Form1Provider implements DefaultGroupSequenceProvider<Test7Form1> {

    /**
     * 1: 访客
     * 2: 领导
     * 3: 管理员
     */
    private final static List<Integer> roleList = Arrays.asList(1, 2, 3);

    @Override
    public List<Class<?>> getValidationGroups(Test7Form1 test7Form1) {

        List<Class<?>> defaultGroupSequence = new ArrayList<>();
        defaultGroupSequence.add(Test7Form1.class);

        if (ObjectUtils.isEmpty(test7Form1)) {
            return defaultGroupSequence;
        }

        // 获取角色code
        Integer role = Optional.ofNullable(test7Form1.getRole()).orElse(0) ;
        if (!roleList.contains(role)) {
            return defaultGroupSequence;
        }

        // 根据角色code,开启相应的组校验
        if (role == 1) {
            defaultGroupSequence.add(Test7Form1.GuestGroup.class);
        } else if (role == 2) {
            defaultGroupSequence.add(Test7Form1.LeaderGroup.class);
        } else if (role == 3) {
            defaultGroupSequence.add(Test7Form1.AdminGroup.class);
        }

        return defaultGroupSequence;
    }
}

⏹Controller层进行校验

@Controller
@RequestMapping("/test7")
public class Test7Controller {

    @Resource
    private LocalValidatorFactoryBean validator;

    @GetMapping("/init")
    public ModelAndView init() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("test7");
        return  modelAndView;
    }

    @PostMapping("/groupSequenceProvider")
    @ResponseBody
    public void groupSequenceProvider(@RequestBody Test7Form1 form) {

        Set<ConstraintViolation<Test7Form1>> validate = validator.validate(form);
        for (ConstraintViolation<Test7Form1> bean : validate) {

            // 获取当前的校验信息
            String message = bean.getMessage();
            System.out.println(message);
        }
    }
}

当角色为2(领导)时,最多只能有4个权限,因此显示校验信息

当角色为1(访客)时,最多只能有2个权限,因此显示校验信息

当角色为3(管理员)时,最多有10个权限,因此无校验信息

加载全部内容

相关教程
猜你喜欢
用户评论