3

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?

skyho
  • 1,438
  • 2
  • 20
  • 47

2 Answers2

4

The validation needs to be triggered somewhere, for entities in your case the spring framework does it (or jpa perhaps). DTOs never make it there. So you have to trigger the validation (validator.validate) on your own, as per the documentation. Here's a related question asking at which application layer to do it.

Curiosa Globunznik
  • 3,129
  • 1
  • 16
  • 24
-1
 @PostMapping("/test")
    public void test( @RequestBody @Valid final UserDto dto) {

        // logic
    }

u should add @Valid annotation in controller. If u want validate dto in service layers u should add @Validate and @Valid :

@Service
@Validated
public class Service {

   public void test(@Valid UserDto dto){
       // logic
   }
}