• Java使用Validator类进行实体属性格式验证(一次性返回所有验证结果)


    一、场景

      在开发过程中,经常需要对自定义组装的类进行字段校验,因此专门分装了一个对实体类属性格式验证的方法。

    二、工具代码

    /**
     * 功能:实体属性格式验证
     * 

    * ────────────────────────────────────────── * version 变更日期 修改人 修改说明 * ------------------------------------------ * V1.0.0 2022/8/15 CHY 初版 * ────────────────────────────────────────── * * @author CHY */ public class ValidatorUtil { private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); /** * 实体属性格式验证(一次性返回所有验证结果) * * @param t 实体集 * @param 实体 * @return 返回结果 */ public static <T> String validates(T t) { Validator validator = factory.getValidator(); Set<ConstraintViolation<T>> constraintViolations = validator.validate(t); List<String> messageList = new ArrayList<>(); for (ConstraintViolation<T> constraintViolation : constraintViolations) { messageList.add(constraintViolation.getMessage()); } String error = ""; if (messageList != null && messageList.size() > 0) { error = StringUtils.join(messageList, ";"); } return error; } }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    三、实体类代码

    @Data
    public class User {
    
        @Length(max = 10)
        @NotBlank(message = "用户姓名不能为空")
        private String name;
    
        @ApiModelProperty("手机号")
        @Length(min = 1, max = 12)
        @NotBlank(message = "手机号码不能为空")
        @Pattern(regexp = "^1(3\\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$",message = "手机号码格式不正确")
        private String mobile;
    
        @ApiModelProperty("身份证号")
        @NotBlank(message = "身份证号不能为空")
        private String idCard;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    四、如何使用?

    public class ValidatorUtilTest {
    
        public static void main(String[] args) {
            User user = new User();
            user.setName("");
            user.setMobile("4561253");
            user.setIdCard("11111111111111111111111111111111111111");
            String validates = validates(user);
            if (!StringUtil.isEmpty(validates)) {
            	// TODO 在实际开发中,此处可以替换成抛出异常
                System.out.println("错误信息如下:" + validates);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    五、测试结果

    在这里插入图片描述

  • 相关阅读:
    常见的几种身份验证方法
    找到一个超级神奇,百试百灵的解决 ModuleNotFoundError: No module named xxx 的方法
    音频转文字软件哪个好?建议收藏这些软件
    Vue 中可重用组件的 3 个主要问题
    Paxos Made Simple
    JAVA设计模式-责任链模式
    解决 matplotlib 中文字体无法显示问题
    单纯形法实现(GUI based on Matlab)全部的源代码
    计算机毕业设计java毕业设计项目源代码精品SSM学生选课系统[包运行成功]
    【Unity Texture】_MainTex的含义
  • 原文地址:https://blog.csdn.net/Amber_1/article/details/126350205