加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

mapping – 自定义转换器上的Dozer,InstantiationException

发布时间:2020-12-15 05:09:59 所属栏目:Java 来源:网络整理
导读:我写了自己的客户转换器: public class MyFancyCustomConverter extends DozerConverterInteger,AnObject{ public MyFancyCustomConverter(ClassInteger prototypeA,ClassAnObject prototypeB) { super(prototypeA,prototypeB); } @Override public AnObjec
我写了自己的客户转换器:

public class MyFancyCustomConverter extends DozerConverter<Integer,AnObject>
{
    public MyFancyCustomConverter(Class<Integer> prototypeA,Class<AnObject> prototypeB)
    {
        super(prototypeA,prototypeB);
    }

    @Override
    public AnObject convertTo(Integer source,AnObject destination)
    {
        // TODO: do something
        return null;
    }

    @Override
    public Integer convertFrom(AnObject source,Integer destination)
    {
        // TODO: do something
        return 0;
    }
}

我的mapping.xml:

<mapping>
    <class-a>java.lang.Integer</class-a>
    <class-b>xyz.AnObject</class-b>
    <field custom-converter="xyz.MyFancyCustomConverter" custom-converter-param="hello">
      <a>this</a>
      <b key="my.key">this</b>
    </field>
</mapping>

但是我得到了这个例外:

org.dozer.MappingException:java.lang.InstantiationException:xyz.MyFancyCustomConverter

知道我做错了什么吗?我想这是因为MyFancyCustomConverter没有默认转换器.但是我不能添加一个,因为DozerConverter没有…

解决方法

public MyFancyCustomConverter(Class<Integer> prototypeA,Class<AnObject> prototypeB)
{
    super(prototypeA,prototypeB);
}

应该

public MyFancyCustomConverter()
{
    super(Integer.class,AnObject.class);
}

超类需要知道这两个类的运行时类型,并且由于类型擦除,需要传入一个类型标记.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读