简体   繁体   中英

Dozer, InstantiationException on custom converter

I've wrote my own customer converter:

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

And my 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>

But I get this Exception:

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

Any idea what I'm doing wrong? I guess it's because MyFancyCustomConverter doesn't have a default converter. But I can't add one, because DozerConverter doesn't have one...

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

Should be

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

The superclass needs to know the runtime type of the two classes, and because of type erasure, needs a type token to be passed in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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