可访问推土机映射类级别 [英] Dozer Mapping Class level is-accessible

查看:90
本文介绍了可访问推土机映射类级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用推土机框架克隆对象。我希望推土机框架无需使用getter和setter即可克隆数据,为此,我在类级别设置了is-accessible属性。但这似乎不起作用。

I am using dozer framework for cloning my objects. I want dozer framework to clone the data without using the getters and setters and for this I am setting the is-accessible property at the class level. But this does not seem to work. When I set is-accessible at field level it works fine.

BeanMappingBuilder builder = new BeanMappingBuilder(){

            @Override
            protected void configure() {
                mapping(type(A.class).accessible(true),type(A.class).accessible(true)).exclude("field1").exclude("field2");
                            }

        };
        m.addMapping(builder);

我想使用的原因是可访问的,因为我在A类中声明了一个字段

The reason why I wanted use is-accessible is because I have a field in class A which is declared as

private SortedSet<String> foo;

但是吸气剂就像

public Collection<String> getFoo()
{
      return foo;
}

我认为推土机找不到吸气剂,因为它正在为吸尘器返回不同的类型。 foo字段。有人可以告诉我这是推土机中的错误还是我做错了?

I think dozer cannot find the getter as it is returning a different type for the field foo. Can someone tell me if this is a bug in dozer or Is it something I am doing wrong?

在此先感谢您的帮助!

推荐答案

请注意,如果Dozer在源类中找不到getter而在目标类中找不到setter,则会引发错误。

Note that Dozer throws an error if it cannot find getter in source class and setter in destination class.

Afaik,数据类型的更改不重要,因为它使用了反射。

Afaik, the change in datatype shouldn't matter as it uses reflection.

要回答您的第一个问题,而不是在类级别访问设置,而是仅公开您需要的字段的风险可能较小。例如:

To answer your first question, rather than setting is-accessible at class level, it could be less risky to expose only the field you need. For example:

 DozerBeanMapper dozerBeanMapper = new DozerBeanMapper();
        dozerBeanMapper.addMapping(getBeanMappingBuilder("fieldName",
                                                         SourceClassName.class,
                                                         DestClassName.class));


protected BeanMappingBuilder getBeanMappingBuilder(final String fieldName,
                                                       final Class typeA,
                                                       final Class typeB) {
        return new BeanMappingBuilder() {
            @Override
            protected void configure() {
                mapping(typeA
                    , typeB).fields(
                    field(fieldName).accessible(true), field(fieldName).accessible(true));

            }
        };
    }

这篇关于可访问推土机映射类级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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