@NumberValid注解适配多语种获取失败时的异常处理

This commit is contained in:
gaoqr
2026-01-27 10:53:52 +08:00
parent 0d4700e225
commit 4ddf3d7709
@@ -5,7 +5,6 @@ import org.apache.commons.lang3.StringUtils;
import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext; import jakarta.validation.ConstraintValidatorContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -32,8 +31,11 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
private int fraction; private int fraction;
@Autowired private final MessageSource messageSource;
private MessageSource messageSource;
public NumberValidator(MessageSource messageSource) {
this.messageSource = messageSource;
}
@Override @Override
public void initialize(NumberValid constraintAnnotation) { public void initialize(NumberValid constraintAnnotation) {
@@ -57,8 +59,8 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
notification = handleLong(value.longValue()); notification = handleLong(value.longValue());
} else if (value instanceof Double) { } else if (value instanceof Double) {
notification = handleDouble(value.doubleValue()); notification = handleDouble(value.doubleValue());
} else if (value instanceof BigDecimal) { } else if (value instanceof BigDecimal bigvalue) {
notification = handleBigDecimal((BigDecimal) value); notification = handleBigDecimal(bigvalue);
} }
if (StringUtils.isNotEmpty(notification)) { if (StringUtils.isNotEmpty(notification)) {
context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值 context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值
@@ -78,7 +80,7 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
if (value < Integer.parseInt(ZERO)) { if (value < Integer.parseInt(ZERO)) {
Locale locale = LocaleContextHolder.getLocale(); Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{ return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{
messageSource.getMessage(name, null, locale) getFieldMessage(name, locale)
}, locale); }, locale);
} }
return null; return null;
@@ -94,7 +96,7 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
if (value < Long.parseLong(ZERO)) { if (value < Long.parseLong(ZERO)) {
Locale locale = LocaleContextHolder.getLocale(); Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{ return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{
messageSource.getMessage(name, null, locale) getFieldMessage(name, locale)
}, locale); }, locale);
} }
return null; return null;
@@ -108,7 +110,7 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
*/ */
private String handleDouble(Double value) { private String handleDouble(Double value) {
Locale locale = LocaleContextHolder.getLocale(); Locale locale = LocaleContextHolder.getLocale();
String fieldName = messageSource.getMessage(name, null, locale); String fieldName = getFieldMessage(name, locale);
if (value < Double.parseDouble(ZERO)) { if (value < Double.parseDouble(ZERO)) {
return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{fieldName}, locale); return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{fieldName}, locale);
} }
@@ -142,7 +144,7 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
*/ */
private String handleBigDecimal(BigDecimal value) { private String handleBigDecimal(BigDecimal value) {
Locale locale = LocaleContextHolder.getLocale(); Locale locale = LocaleContextHolder.getLocale();
String fieldName = messageSource.getMessage(name, null, locale); String fieldName = getFieldMessage(name, locale);
if (value.compareTo(BigDecimal.ZERO) < 0) { if (value.compareTo(BigDecimal.ZERO) < 0) {
return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{fieldName}, locale); return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{fieldName}, locale);
} }
@@ -164,4 +166,17 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
} }
return null; return null;
} }
/**
* 获取属性描述语种
*
* @return
*/
private String getFieldMessage(String name, Locale locale) {
try {
return messageSource.getMessage(name, null, locale);
} catch (Exception e) {
return name;
}
}
} }