Dozer Mapping Class level is-accessible

走远了吗. 提交于 2019-12-23 20:37:33

问题


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);

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;

but the getter is like

public Collection<String> getFoo()
{
      return 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?

Thanks in advance for your help!!


回答1:


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

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));

            }
        };
    }


来源:https://stackoverflow.com/questions/25389976/dozer-mapping-class-level-is-accessible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!