I have an annotation set over objects of type dto, the same as over objects of type Entity. The annotation works on entities, but it does not work on objects of type dto.
I work in SpringBoot. application.properties
validate.packageid.size = "The field 'PACKAGEID' can contain only {max} symbols.";
config file
@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;
}
}
dto
@Size(message = "{validate.packageid.size}", max = 36)
private String documentId
entity
@Column(name = "DOCUMENTID")
@Size(message = "{validate.packageid.size}", max = 36)
private String documentId;
I cannot use the annotation @Valid because I fill an object of dto type with reflection technology.
public static <S> S fillData (S object, List<Object> values){
return obtainMetadataOfObject(object, values);
}
I need to be able to get the constraint annotation, or rather its parameters, set on the fields of the dto object (but in the case of the dto object I get null, since Spring may not know what to use the constraint annotations set on the fields of the dto object), in the case of entity - it turns out, but the entity validator is engaged by Spritg, since it manages the entity as a component from the Application context.
To validate the dto on the web client side, I use the @Valid annotation in the parameters of the method that handles the request from the client
For validation dto from
Update
I put the annotation @Validation over dto and after that I got the data I need.
It work for classes that don't have classes-heir.
I get data of annotation @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();
}
But this does not work for objects whose fields are split into several classes : several abstract and one produce.
Validation does not work for composite objects of type DTO, Any help is appreciated?