mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
java bean校验工具
This commit is contained in:
parent
f4d357971b
commit
d6d103dcb5
@ -258,5 +258,20 @@
|
||||
<version>1.2.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>6.0.12.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
<version>3.0.1-b01</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.web</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>2.2.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -0,0 +1,66 @@
|
||||
package cn.hutool.extra.validation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* bean 校验结果
|
||||
*
|
||||
* @author chengqiang
|
||||
*/
|
||||
|
||||
public class BeanValidationResult {
|
||||
|
||||
/**
|
||||
* 校验是否成功
|
||||
*/
|
||||
private Boolean success = Boolean.TRUE;
|
||||
|
||||
/**
|
||||
* 错误消息
|
||||
*/
|
||||
private List<ErrorMessage> errorMessages = new ArrayList<>();
|
||||
|
||||
public Boolean getSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(Boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public List<ErrorMessage> getErrorMessages() {
|
||||
return errorMessages;
|
||||
}
|
||||
|
||||
public void setErrorMessages(List<ErrorMessage> errorMessages) {
|
||||
this.errorMessages = errorMessages;
|
||||
}
|
||||
|
||||
public static class ErrorMessage {
|
||||
/**
|
||||
* 属性字段名称
|
||||
*/
|
||||
private String propertyName;
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
private String message;
|
||||
|
||||
public String getPropertyName() {
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
public void setPropertyName(String propertyName) {
|
||||
this.propertyName = propertyName;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package cn.hutool.extra.validation;
|
||||
|
||||
import cn.hutool.extra.validation.BeanValidationResult.ErrorMessage;
|
||||
import org.hibernate.validator.HibernateValidator;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
/**
|
||||
* java bean 校验工具类
|
||||
*
|
||||
* @author chengqiang
|
||||
*/
|
||||
public class BeanValidationUtil {
|
||||
|
||||
private static final Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(false).buildValidatorFactory().getValidator();
|
||||
|
||||
/**
|
||||
* 校验对象
|
||||
*
|
||||
* @param bean bean
|
||||
* @param groups 校验组
|
||||
* @return {@link Set}
|
||||
*/
|
||||
public static <T> Set<ConstraintViolation<T>> validate(T bean, Class<?>... groups) {
|
||||
return validator.validate(bean, groups);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验对象
|
||||
*
|
||||
* @param bean bean
|
||||
* @param groups 校验组
|
||||
* @return {@link BeanValidationResult}
|
||||
*/
|
||||
public static <T> BeanValidationResult warpValidate(T bean, Class<?>... groups) {
|
||||
return warpBeanValidationResult(validate(bean, groups));
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验bean的某一个属性
|
||||
*
|
||||
* @param bean bean
|
||||
* @param propertyName 属性名称
|
||||
* @return {@link Set}
|
||||
*/
|
||||
public static <T> Set<ConstraintViolation<T>> validateProperty(T bean, String propertyName, Class<?>... groups) {
|
||||
return validator.validateProperty(bean, propertyName, groups);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 校验bean的某一个属性
|
||||
*
|
||||
* @param bean bean
|
||||
* @param propertyName 属性名称
|
||||
* @return {@link BeanValidationResult}
|
||||
*/
|
||||
public static <T> BeanValidationResult warpValidateProperty(T bean, String propertyName, Class<?>... groups) {
|
||||
return warpBeanValidationResult(validateProperty(bean, propertyName, groups));
|
||||
}
|
||||
|
||||
/**
|
||||
* 包装校验结果
|
||||
*
|
||||
* @param constraintViolations 校验结果集
|
||||
* @return {@link BeanValidationResult}
|
||||
*/
|
||||
private static <T> BeanValidationResult warpBeanValidationResult(Set<ConstraintViolation<T>> constraintViolations) {
|
||||
BeanValidationResult result = new BeanValidationResult();
|
||||
for (ConstraintViolation<T> constraintViolation : constraintViolations) {
|
||||
result.setSuccess(Boolean.FALSE);
|
||||
ErrorMessage errorMessage = new ErrorMessage();
|
||||
errorMessage.setPropertyName(constraintViolation.getPropertyPath().toString());
|
||||
errorMessage.setMessage(constraintViolation.getMessage());
|
||||
result.getErrorMessages().add(errorMessage);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* java bean 校验工具
|
||||
*
|
||||
* @author chengqiang
|
||||
*/
|
||||
package cn.hutool.extra.validation;
|
@ -0,0 +1,51 @@
|
||||
package cn.hutool.extra.validation;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* java bean 校验工具类单元测试
|
||||
*
|
||||
* @author chengqiang
|
||||
*/
|
||||
public class BeanValidatorUtilTest {
|
||||
|
||||
public static class TestClass {
|
||||
|
||||
@NotBlank(message = "姓名不能为空")
|
||||
private String name;
|
||||
|
||||
@NotBlank(message = "地址不能为空")
|
||||
private String address;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanValidatorTest() {
|
||||
BeanValidationResult result = BeanValidationUtil.warpValidate(new TestClass());
|
||||
Assert.isTrue(result.getSuccess());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyValidatorTest() {
|
||||
BeanValidationResult result = BeanValidationUtil.warpValidateProperty(new TestClass(), "name");
|
||||
Assert.isTrue(result.getSuccess());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user