推土机将字符串映射到字节数组

时间:2018-12-17 21:33:46

标签: java spring dozer

我正在尝试解决下面的Source类之间的简单转换

package com.my.package;

public class Source
{
   private String member;

   public String getMember()
   {
       return member;
   }

   public void setMember(String member)
   {
       this.member = member;
   }

}

到目的地类别,如下所述:

package com.my.package;

public class Destination
{
   private byte[] member;

   public byte[] getMember()
   {
       return member;
   }

   public void setMember(byte[] member)
   {
       this.member = member;
   }

}

为此,我首先尝试使用以下配置弹出推土机映射器

  <mapping map-id="test">
        <class-a>com.my.package.Source
        </class-a>
        <class-b>com.my.package.Destination
        </class-b>
        <field>
            <a>member</a>
            <b>member</b>
        </field>
    </mapping>

并遇到此异常:

Exception in thread "main" org.dozer.MappingException: Illegal object type for the method 'setMember'. 
 Expected types: 
[B
 Actual types: 
java.lang.String

这似乎很公平,在更多搜索下,我在config下面遇到了

<mapping map-id="test">
        <class-a>com.my.package.Source
        </class-a>
        <class-b>com.my.package.Destination
        </class-b>
        <field>
            <a>member</a>
            <b set-method="setMemberFromString(java.lang.String)">member</b>
        </field>
    </mapping>

并在Destination类中引入了这个新的setter,类似:

public void setMemberFromString(String str) {
   this.member = str.getBytes(Charset.forName("UTF-8"));
}

令人惊讶的是,即使这样也不起作用,并且给出了相同的例外。知道我想念什么吗?

0 个答案:

没有答案