Java – Why validation does not work for objects of type DTO, but only for entities

Why validation does not work for objects of type DTO, but only for entities… here is a solution to the problem.

Why validation does not work for objects of type DTO, but only for entities

I set annotations on objects of type dto versus annotations on objects of type Entity. Annotations apply to entities, but not to objects of type dto.

I work at SpringBoot.
application.properties

validate.packageid.size = "The field 'PACKAGEID' can contain only {max} symbols.";

Configuration 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

Entities

@Column(name = "DOCUMENTID")
@Size(message = "{validate.packageid.size}", max = 36)
private String documentId;

I

can’t use annotation @Valid because I’m filling an object of type dto with reflection techniques.

<pre class=”lang-java prettyprint-override”>public static <S> S fillData (S object, List<Object> values){
return obtainMetadataOfObject(object, values);
}

I need to be able to set constraint annotations on the fields of the dto object,

or rather its parameters (but in the case of dto objects, I get null because Spring may not know what constraint to use to set annotations on the fields of the dto object), in the case of entities – it turns out that the entity validator is involved by Spritg because it manages the entity as a component from the application context.

To validate the dto in the web client, I used @Valid annotations in the parameters of the method that handles the client request

Used to verify that dto comes from

Update

I

put the comment @Validation on the dto and after that I got the data I needed.

It applies to classes that do not have class heirs.

Get the annotation data @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 apply to objects where the field is divided into several classes: several abstract classes and a generated class.

Validation doesn’t work with compound objects of type DTO, do I need any help?

Solution

Validation needs to be triggered somewhere, and for the entity in your case, the Spring Framework does this (or maybe JPA). DTOs never succeed. Therefore, you must base it on documentation Trigger validation itself (validator.validate). related question Ask which application tier to do this at.

Related Problems and Solutions