mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
调整样式
This commit is contained in:
parent
d6d103dcb5
commit
e9a3617690
@ -11,56 +11,56 @@ import java.util.List;
|
||||
|
||||
public class BeanValidationResult {
|
||||
|
||||
/**
|
||||
* 校验是否成功
|
||||
*/
|
||||
private Boolean success = Boolean.TRUE;
|
||||
/**
|
||||
* 校验是否成功
|
||||
*/
|
||||
private Boolean success = Boolean.TRUE;
|
||||
|
||||
/**
|
||||
* 错误消息
|
||||
*/
|
||||
private List<ErrorMessage> errorMessages = new ArrayList<>();
|
||||
/**
|
||||
* 错误消息
|
||||
*/
|
||||
private List<ErrorMessage> errorMessages = new ArrayList<>();
|
||||
|
||||
public Boolean getSuccess() {
|
||||
return success;
|
||||
}
|
||||
public Boolean getSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(Boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
public void setSuccess(Boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public List<ErrorMessage> getErrorMessages() {
|
||||
return errorMessages;
|
||||
}
|
||||
public List<ErrorMessage> getErrorMessages() {
|
||||
return errorMessages;
|
||||
}
|
||||
|
||||
public void setErrorMessages(List<ErrorMessage> errorMessages) {
|
||||
this.errorMessages = errorMessages;
|
||||
}
|
||||
public void setErrorMessages(List<ErrorMessage> errorMessages) {
|
||||
this.errorMessages = errorMessages;
|
||||
}
|
||||
|
||||
public static class ErrorMessage {
|
||||
/**
|
||||
* 属性字段名称
|
||||
*/
|
||||
private String propertyName;
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
private String message;
|
||||
public static class ErrorMessage {
|
||||
/**
|
||||
* 属性字段名称
|
||||
*/
|
||||
private String propertyName;
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
private String message;
|
||||
|
||||
public String getPropertyName() {
|
||||
return propertyName;
|
||||
}
|
||||
public String getPropertyName() {
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
public void setPropertyName(String propertyName) {
|
||||
this.propertyName = propertyName;
|
||||
}
|
||||
public void setPropertyName(String propertyName) {
|
||||
this.propertyName = propertyName;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,69 +16,68 @@ import java.util.Set;
|
||||
*/
|
||||
public class BeanValidationUtil {
|
||||
|
||||
private static final Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(false).buildValidatorFactory().getValidator();
|
||||
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 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));
|
||||
}
|
||||
/**
|
||||
* 校验对象
|
||||
*
|
||||
* @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 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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验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;
|
||||
}
|
||||
/**
|
||||
* 包装校验结果
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,40 +12,40 @@ import javax.validation.constraints.NotBlank;
|
||||
*/
|
||||
public class BeanValidatorUtilTest {
|
||||
|
||||
public static class TestClass {
|
||||
public static class TestClass {
|
||||
|
||||
@NotBlank(message = "姓名不能为空")
|
||||
private String name;
|
||||
@NotBlank(message = "姓名不能为空")
|
||||
private String name;
|
||||
|
||||
@NotBlank(message = "地址不能为空")
|
||||
private String address;
|
||||
@NotBlank(message = "地址不能为空")
|
||||
private String address;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = 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 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());
|
||||
}
|
||||
@Test
|
||||
public void propertyValidatorTest() {
|
||||
BeanValidationResult result = BeanValidationUtil.warpValidateProperty(new TestClass(), "name");
|
||||
Assert.isTrue(result.getSuccess());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user