refactor 移除ValidateAnnotationHandler的getMsg方法并修改相关实现类

This commit is contained in:
zefeng_java 2021-05-17 10:36:38 +08:00
parent 255aede037
commit 670e46a740
7 changed files with 15 additions and 32 deletions

View File

@ -14,6 +14,8 @@ import java.util.Date;
*/ */
public class MaxHandler implements ValidateAnnotationHandler<Max> { public class MaxHandler implements ValidateAnnotationHandler<Max> {
private static final String MSG = "的值必须小于{}!";
@Override @Override
public Class<Max> getAnnotation() { public Class<Max> getAnnotation() {
return Max.class; return Max.class;
@ -35,13 +37,9 @@ public class MaxHandler implements ValidateAnnotationHandler<Max> {
valid = true; valid = true;
} }
if (!valid) { if (!valid) {
MessageFormatter.format(getResultMsgWhenInvalid(), value).getMessage(); MessageFormatter.format(MSG, value).getMessage();
} }
return null; return null;
} }
@Override
public String getResultMsgWhenInvalid() {
return "的值必须小于{}!";
}
} }

View File

@ -14,6 +14,8 @@ import java.util.Date;
*/ */
public class MinHandler implements ValidateAnnotationHandler<Min> { public class MinHandler implements ValidateAnnotationHandler<Min> {
private static final String MSG = "的值必须大于{}!";
@Override @Override
public Class<Min> getAnnotation() { public Class<Min> getAnnotation() {
return Min.class; return Min.class;
@ -35,13 +37,9 @@ public class MinHandler implements ValidateAnnotationHandler<Min> {
valid = true; valid = true;
} }
if (!valid) { if (!valid) {
MessageFormatter.format(getResultMsgWhenInvalid(), value).getMessage(); MessageFormatter.format(MSG, value).getMessage();
} }
return null; return null;
} }
@Override
public String getResultMsgWhenInvalid() {
return "的值必须小于{}!";
}
} }

View File

@ -12,6 +12,8 @@ import javax.validation.constraints.NotBlank;
*/ */
public class NotBlankHandler implements ValidateAnnotationHandler<NotBlank> { public class NotBlankHandler implements ValidateAnnotationHandler<NotBlank> {
private static final String MSG = "格式错误或者没有有效字符!";
@Override @Override
public Class<NotBlank> getAnnotation() { public Class<NotBlank> getAnnotation() {
return NotBlank.class; return NotBlank.class;
@ -20,13 +22,9 @@ public class NotBlankHandler implements ValidateAnnotationHandler<NotBlank> {
@Override @Override
public String validate(NotBlank validateAnnotation, Object fieldValue) { public String validate(NotBlank validateAnnotation, Object fieldValue) {
if(StrUtil.isBlankIfStr(fieldValue)) { if(StrUtil.isBlankIfStr(fieldValue)) {
return getResultMsgWhenInvalid(); return MSG;
} }
return null; return null;
} }
@Override
public String getResultMsgWhenInvalid() {
return "格式错误或者没有有效字符!";
}
} }

View File

@ -12,6 +12,8 @@ import javax.validation.constraints.NotEmpty;
*/ */
public class NotEmptyHandler implements ValidateAnnotationHandler<NotEmpty> { public class NotEmptyHandler implements ValidateAnnotationHandler<NotEmpty> {
private static final String MSG = "不能为空!";
@Override @Override
public Class<NotEmpty> getAnnotation() { public Class<NotEmpty> getAnnotation() {
return NotEmpty.class; return NotEmpty.class;
@ -20,13 +22,8 @@ public class NotEmptyHandler implements ValidateAnnotationHandler<NotEmpty> {
@Override @Override
public String validate(NotEmpty validateAnnotation, Object fieldValue) { public String validate(NotEmpty validateAnnotation, Object fieldValue) {
if(ObjectUtil.isEmpty(fieldValue)) { if(ObjectUtil.isEmpty(fieldValue)) {
return getResultMsgWhenInvalid(); return MSG;
} }
return null; return null;
} }
@Override
public String getResultMsgWhenInvalid() {
return "不能为空!!!!";
}
} }

View File

@ -13,6 +13,7 @@ import javax.validation.constraints.NotNull;
*/ */
public class NotNullHandler implements ValidateAnnotationHandler<NotNull> { public class NotNullHandler implements ValidateAnnotationHandler<NotNull> {
private static final String MSG = "必传!";
@Override @Override
public Class<NotNull> getAnnotation() { public Class<NotNull> getAnnotation() {
@ -22,13 +23,8 @@ public class NotNullHandler implements ValidateAnnotationHandler<NotNull> {
@Override @Override
public String validate(NotNull validateAnnotation, Object fieldValue) { public String validate(NotNull validateAnnotation, Object fieldValue) {
if (ObjectUtil.isNull(fieldValue)) { if (ObjectUtil.isNull(fieldValue)) {
return getResultMsgWhenInvalid(); return MSG;
} }
return null; return null;
} }
@Override
public String getResultMsgWhenInvalid() {
return "必传!";
}
} }

View File

@ -57,9 +57,5 @@ public interface ValidateAnnotationHandler<T extends Annotation> {
*/ */
String validate(T validateAnnotation, Object fieldValue); String validate(T validateAnnotation, Object fieldValue);
/**
* @return 消息模板
*/
String getResultMsgWhenInvalid();
} }

View File

@ -112,7 +112,7 @@ public class ValidateHandlerHelper {
} else { } else {
fieldComment = field.getName(); fieldComment = field.getName();
} }
String msg = fieldComment + handlerOptional.get().getResultMsgWhenInvalid(); String msg = fieldComment + validate;
CustomConstraintDescriptor<?> customConstraintDescriptor = CustomConstraintDescriptor<?> customConstraintDescriptor =
new CustomConstraintDescriptor<>(field.getAnnotation(handlerOptional.get().getAnnotation())); new CustomConstraintDescriptor<>(field.getAnnotation(handlerOptional.get().getAnnotation()));
return CustomConstraintViolation.of(rootBean, msg, propertyPath, fieldValue, customConstraintDescriptor); return CustomConstraintViolation.of(rootBean, msg, propertyPath, fieldValue, customConstraintDescriptor);