繁体   English   中英

推土机深度映射不起作用

[英]Dozer deep mapping not working

我正在尝试使用dozer 4.1在类之间进行映射。 我有一个看起来像这样的源类:

    public class initRequest{
     protected String id;
     protected String[] details
}

我有一个目标类,如下所示:

public class initResponse{
       protected String id;
       protected DetailsObject detObj;
}

public class DetailsObject{
 protected List<String>  details;
}

所以本质上我想将details数组中的字符串填充到Details对象中的List中。

我已经尝试过这样的映射:

<mapping wildcard="true" >
  <class-a>initRequest</class-a>
  <class-b>initResponse</class-b>   
  <field>
    <a is-accessible="true">details</a>
    <b is-accessible="true">detObj.details</b>
  </field>
</mapping>

但是我得到这个错误:

Exception in thread "main" net.sf.dozer.util.mapping.MappingException: java.lang.NoSuchFieldException: detObj.details
    at net.sf.dozer.util.mapping.util.MappingUtils.throwMappingException(MappingUtils.java:91)
    at net.sf.dozer.util.mapping.propertydescriptor.FieldPropertyDescriptor.<init>(FieldPropertyDescriptor.java:43)
    at net.sf.dozer.util.mapping.propertydescriptor.PropertyDescriptorFactory.getPropertyDescriptor(PropertyDescriptorFactory.java:53)
    at net.sf.dozer.util.mapping.fieldmap.FieldMap.getDestPropertyDescriptor(FieldMap.java:370)
    at net.sf.dozer.util.mapping.fieldmap.FieldMap.getDestFieldType(FieldMap.java:103)
    at net.sf.dozer.util.mapping.util.MappingsParser.processMappings(MappingsParser.java:95)
    at net.sf.dozer.util.mapping.util.CustomMappingsLoader.load(CustomMappingsLoader.java:77)
    at net.sf.dozer.util.mapping.DozerBeanMapper.loadCustomMappings(DozerBeanMapper.java:149)
    at net.sf.dozer.util.mapping.DozerBeanMapper.getMappingProcessor(DozerBeanMapper.java:132)
    at net.sf.dozer.util.mapping.DozerBeanMapper.map(DozerBeanMapper.java:94)

我该如何映射它才能起作用?

这对我有用。 我正在使用5.2.1版本

 <mapping wildcard="true" >
      <class-a>initRequest</class-a>
      <class-b>initResponse</class-b>   
      <field>
        <a>details</a>
        <b is-accessible="true">detObj.details</b>
      </field>
    </mapping>

请注意,不需要“可访问”。 希望能帮助到你

问题解决了...

  • is-accesible允许不考虑访问修饰符和getter / setter的存在而更新对象(对于使用JAXB生成的对象必不可少)

  • 深度映射的“点”表示法可用于访问嵌套对象

将两者结合是一项功能,在Dozer中不起作用(也许在较新的版本中有效)

解决方案...修改xsd,以便不需要深度映射。 这不是我理想的解决方案,但是比为每个对象编写自定义转换器要好

对于JaxB,用户可以下载并使用该插件来生成设置器。 请参阅此链接以获取更多详细信息, https://jaxb2-commons.dev.java.net/collection-setter-injector/

尽管您似乎无法将“可访问”和点表示法结合使用,但另一种方法是将深度映射分解为较小的映射。

我们用JAX-WS生成的代码遇到了这种情况。 您有没有设置方法的列表,在我们的例子中,它们是深层嵌套的。 通过简单地将大型深度映射分解为较小的映射,从而“找到”所需的路径,我们找到了解决方案。 我试图在我的博客中对此进行解释:

http://btarlton.blogspot.com/2014/08/dozer-deep-nestinga-different-approach.html

但是,诀窍只是通过较小的映射遍历对象树,并在需要访问不带setter的列表时使用is-accessible =“ true”,并使用“ this”作为属性名称来保持源代码的传递。

希望这可以帮助!

我猜想访问器(getter / setter)丢失了。 顺便说一句,我认为您还需要为DetailsObject提供一个空的构造函数,以便推土机可以实例化它。

总结一下,此问题有以下几种选择:1)使用JaxB插件启用Naveen讨论的设置程序2)此类属性使用可访问

我相信使用第一种方法会不必要地公开集合/列表的设置器,因为您可能会设置为null。

我们决定为此类字段(而不是整个类)启用“可访问”,以避免任何副作用。

我已经在Dozer Mapping Class级别上讨论了可访问的解决方案

<b is-accessible="true">detObj.details</b>

应该换成

<b is-accessible="true">DetailsObject.details</b>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM