为什么验证对DTO类型的对象不起作用,而仅对实体起作用 [英] Why does validation not work on objects of type DTO, but only on entities

查看:192
本文介绍了为什么验证对DTO类型的对象不起作用,而仅对实体起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 dto 类型的对象上设置了注释,与在 Entity 类型的对象上设置了注释.注释适用于实体,但不适用于 dto 类型的对象.

我在 SpringBoot 中工作. application.properties

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

配置文件

 @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
 

实体

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

我无法使用注释 @Valid ,因为我使用反射技术填充了 dto 类型的对象.

 public static  <S> S fillData (S object, List<Object> values){
    return obtainMetadataOfObject(object, values);
}
 

我需要能够获取在dto对象的字段上设置的约束注释,或者更确切地说,它的参数(但是在dto对象的情况下,我会为null,因为Spring可能不知道该使用什么约束事实证明,在dto对象的字段上设置的注释)-事实证明,但Spritg使用了实体验证器,因为它将实体作为Application上下文中的组件进行管理.

要在Web客户端上验证dto,我在处理来自客户端的请求的方法的参数中使用 @Valid 批注

用于

的dto验证

更新

我将注释 @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();

        }
 

但是不适用于对象,这些对象的字段分为几个类:几个抽象类和一个农产品.

验证不适用于 DTO 类型的复合对象,需要任何帮助吗?

解决方案

验证需要在某个地方触发,对于您所使用的实体,spring框架可以进行验证(或者jpa). DTO从来没有做到这一点.因此,您必须根据相关问题,询问在哪个应用程序层执行此操作./p>

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?

解决方案

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.

这篇关于为什么验证对DTO类型的对象不起作用,而仅对实体起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆