普通Java项目使用Hibernate Validator进行数据校验参数校验 校验Bean
背景#
最近有负责维护一个专门做对外数据接口的项目,由于接口数据需要加密,而且解密后的数据需要校验,手动ifelse判断非常繁琐,因此想使用hibernate validator在数据解密后手动进行校验。
依赖#
需要使用以下几个依赖,这里我是用的是maven,如果没有使用maven可以手动下载jar包引入。
需要注意的一点是,如果你使用的是Jdk8,hibernate-validator不要使用7以上的版本。
以及,在某些情况下可能需要引入jboss-logging包(如果报错)
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml/classmate -->
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.el/javax.el-api -->
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.web/el-impl -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
实体类添加注解校验#
如果需要对嵌套的实体类也进行校验,需要在属性上加上@Valid注解,比如下面的equipments
@Data
@Accessors(chain = true)
public class Gamer {
@NotBlank(message = "角色名称不能为空")
private String name;
@Range(min = 0, max = 1, message = "性别只能是0或1")
private Integer sex;
@Min(value = 1, message = "攻击力最低为1")
private Integer attack;
@Min(value = 1, message = "防御力最低为1")
private Integer defense;
@Valid
@Size(min = 1, message = "装备列表不能空")
private List<Equipment> equipments = new ArrayList<>();
}
@Data
@Accessors(chain = true)
public class Equipment {
@NotBlank(message = "装备名称不能为空")
private String name;
@NotNull(message = "装备类型不能为空")
private Integer equipmentType;
}
编写校验工具类#
public class ValidationUtil {
// 如果只需要返回第一个校验失败的信息,需要将下面的failFast设置为true
private static Validator validator = Validation
.byProvider(HibernateValidator.class)
.configure()
.failFast(false)
.buildValidatorFactory()
.getValidator();
public static <T> void validate(T bean) {
Set<ConstraintViolation<T>> validate = validator.validate(bean);
Iterator<ConstraintViolation<T>> it = validate.iterator();
for (; it.hasNext(); ) {
ConstraintViolation<T> next = it.next();
System.out.println(next);
}
}
}
测试#
编写测试类进行测试
public class ValidationUtilTest {
@Test
public void testValidate() {
Gamer gamer = new Gamer();
gamer.setName("小红帽");
gamer.setAttack(-1);
gamer.setDefense(10);
gamer.setSex(0);
Equipment equipment = new Equipment().setEquipmentType(1);
gamer.getEquipments().add(equipment);
ValidationUtil.validate(gamer);
}
}
结果#
作者:马卡龙MK
出处:https://www.cnblogs.com/montaro/p/15834104.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)