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 {
|
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() {
|
public Boolean getSuccess() {
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSuccess(Boolean success) {
|
public void setSuccess(Boolean success) {
|
||||||
this.success = success;
|
this.success = success;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ErrorMessage> getErrorMessages() {
|
public List<ErrorMessage> getErrorMessages() {
|
||||||
return errorMessages;
|
return errorMessages;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setErrorMessages(List<ErrorMessage> errorMessages) {
|
public void setErrorMessages(List<ErrorMessage> errorMessages) {
|
||||||
this.errorMessages = errorMessages;
|
this.errorMessages = errorMessages;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ErrorMessage {
|
public static class ErrorMessage {
|
||||||
/**
|
/**
|
||||||
* 属性字段名称
|
* 属性字段名称
|
||||||
*/
|
*/
|
||||||
private String propertyName;
|
private String propertyName;
|
||||||
/**
|
/**
|
||||||
* 错误信息
|
* 错误信息
|
||||||
*/
|
*/
|
||||||
private String message;
|
private String message;
|
||||||
|
|
||||||
public String getPropertyName() {
|
public String getPropertyName() {
|
||||||
return propertyName;
|
return propertyName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPropertyName(String propertyName) {
|
public void setPropertyName(String propertyName) {
|
||||||
this.propertyName = propertyName;
|
this.propertyName = propertyName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMessage() {
|
public String getMessage() {
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMessage(String message) {
|
public void setMessage(String message) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,69 +16,68 @@ import java.util.Set;
|
|||||||
*/
|
*/
|
||||||
public class BeanValidationUtil {
|
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 bean bean
|
||||||
* @param groups 校验组
|
* @param groups 校验组
|
||||||
* @return {@link Set}
|
* @return {@link Set}
|
||||||
*/
|
*/
|
||||||
public static <T> Set<ConstraintViolation<T>> validate(T bean, Class<?>... groups) {
|
public static <T> Set<ConstraintViolation<T>> validate(T bean, Class<?>... groups) {
|
||||||
return validator.validate(bean, groups);
|
return validator.validate(bean, groups);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验对象
|
* 校验对象
|
||||||
*
|
*
|
||||||
* @param bean bean
|
* @param bean bean
|
||||||
* @param groups 校验组
|
* @param groups 校验组
|
||||||
* @return {@link BeanValidationResult}
|
* @return {@link BeanValidationResult}
|
||||||
*/
|
*/
|
||||||
public static <T> BeanValidationResult warpValidate(T bean, Class<?>... groups) {
|
public static <T> BeanValidationResult warpValidate(T bean, Class<?>... groups) {
|
||||||
return warpBeanValidationResult(validate(bean, groups));
|
return warpBeanValidationResult(validate(bean, groups));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验bean的某一个属性
|
* 校验bean的某一个属性
|
||||||
*
|
*
|
||||||
* @param bean bean
|
* @param bean bean
|
||||||
* @param propertyName 属性名称
|
* @param propertyName 属性名称
|
||||||
* @return {@link Set}
|
* @return {@link Set}
|
||||||
*/
|
*/
|
||||||
public static <T> Set<ConstraintViolation<T>> validateProperty(T bean, String propertyName, Class<?>... groups) {
|
public static <T> Set<ConstraintViolation<T>> validateProperty(T bean, String propertyName, Class<?>... groups) {
|
||||||
return validator.validateProperty(bean, propertyName, 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 constraintViolations 校验结果集
|
||||||
* @param propertyName 属性名称
|
* @return {@link BeanValidationResult}
|
||||||
* @return {@link BeanValidationResult}
|
*/
|
||||||
*/
|
private static <T> BeanValidationResult warpBeanValidationResult(Set<ConstraintViolation<T>> constraintViolations) {
|
||||||
public static <T> BeanValidationResult warpValidateProperty(T bean, String propertyName, Class<?>... groups) {
|
BeanValidationResult result = new BeanValidationResult();
|
||||||
return warpBeanValidationResult(validateProperty(bean, propertyName, groups));
|
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);
|
||||||
* @param constraintViolations 校验结果集
|
}
|
||||||
* @return {@link BeanValidationResult}
|
return result;
|
||||||
*/
|
}
|
||||||
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 class BeanValidatorUtilTest {
|
||||||
|
|
||||||
public static class TestClass {
|
public static class TestClass {
|
||||||
|
|
||||||
@NotBlank(message = "姓名不能为空")
|
@NotBlank(message = "姓名不能为空")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@NotBlank(message = "地址不能为空")
|
@NotBlank(message = "地址不能为空")
|
||||||
private String address;
|
private String address;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAddress(String address) {
|
public void setAddress(String address) {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void beanValidatorTest() {
|
public void beanValidatorTest() {
|
||||||
BeanValidationResult result = BeanValidationUtil.warpValidate(new TestClass());
|
BeanValidationResult result = BeanValidationUtil.warpValidate(new TestClass());
|
||||||
Assert.isTrue(result.getSuccess());
|
Assert.isTrue(result.getSuccess());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void propertyValidatorTest() {
|
public void propertyValidatorTest() {
|
||||||
BeanValidationResult result = BeanValidationUtil.warpValidateProperty(new TestClass(), "name");
|
BeanValidationResult result = BeanValidationUtil.warpValidateProperty(new TestClass(), "name");
|
||||||
Assert.isTrue(result.getSuccess());
|
Assert.isTrue(result.getSuccess());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user