Hibernate 如何获取对象字段上方@Size注释的值
我需要从@Size注释中提取属性值Hibernate 如何获取对象字段上方@Size注释的值,hibernate,spring-boot,reflection,bean-validation,dto,Hibernate,Spring Boot,Reflection,Bean Validation,Dto,我需要从@Size注释中提取属性值 private static int getMaxLimitSize(Field field){ field.setAccessible(true); Size annotation = field.getAnnotation(Size.class); int zero = 0; if(annotation == null) return zero; return annotation.max(); } 这里
private static int getMaxLimitSize(Field field){
field.setAccessible(true);
Size annotation = field.getAnnotation(Size.class);
int zero = 0;
if(annotation == null) return zero;
return annotation.max();
}
这里是dto类型对象字段上的@Size约束
@Size(message = "{validate.documentid.size}", max = 36)
private String documentId;
但是当我尝试获取注释时,我得到null
Size annotation = null;
此注释位于类型为dto的对象上方,并且未被处理,但当处理实体时,此注释可见
使用弹簧靴
错误消息位于文件-ValidationMessages.propertys中
@Configuration
public class ServiceConfig implements WebMvcConfigurer {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setDefaultEncoding("UTF-8");
source.setBasename("classpath:ValidationMessages");
return source;
}
@Nullable
@Override
public Validator getValidator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(messageSource());
return validator;
}
}
下面是在方法-getMaxLimitSize()的类型字段中传递的内容
private static void processLimitValidateForField(Field field, Object object, List<Object> values){
Class<?> type = field.getType();
String typeName = type.getSimpleName();
boolean isTypeClassOfString = isStringType(typeName);
int limitString = 0;
if(isTypeClassOfString){
limitString = getMaxLimitSize(field);
setValue(field, typeName, object, values, limitString);
} else {
setValue(field, typeName, object, values, limitString);
}
}
private static void processLimitValidateForField(字段字段、对象对象、列表值){
类类型=field.getType();
字符串typeName=type.getSimpleName();
布尔值isTypeClassOfString=isStringType(typeName);
int limitString=0;
if(isTypeClassOfString){
limitString=getMaxLimitSize(字段);
setValue(字段、类型名、对象、值、限制字符串);
}否则{
setValue(字段、类型名、对象、值、限制字符串);
}
}
请解释为什么验证程序看不到dto对象,或者为什么反射看不到验证注释,以及可以做什么
如何解决此问题?通过调用方法
field\get(currentObject)
我该怎么办?我传递了一个对象,试图获取一个对象,结果为空。
对象
是谁拥有您想要获取其值JIC的字段。?你想做什么?
if (field.isAccessible()) {
Object someValue = field.get(object);
}