Dozer- Map to Object Array conversion failing with custom converter

14 Views Asked by At

I need to convert to Map<String,String> to KeyValuePair[] using dozer. Despite, multiple efforts i'm unable to do it. Below is what i have:

public class EmployeeSource {
    private String id;
    private String name;
    private Map<String, String> kvMap;
   //removed getters and setters for brevity
....
}

public class EmployeeTarget {
    private String id;
    private String name;
    private KeyValuePair[] kvMap;
     //removed getters and setters for brevity
}

Below is my KeyValuePair class

public class KeyValuePair implements Serializable{
    private String key;
    private String value;

    public KeyValuePair(){
        super();
    }
    
    public KeyValuePair(String key,String value){
        this.key=key;
        this.value=value;
    }
//removed getters and setters for brevity
}

My Custom Converter:

public class MapToKeyValuePairArrayConverter extends DozerConverter<KeyValuePair[],Map<String,String>>{

    
    public MapToKeyValuePairArrayConverter(Class<KeyValuePair[]> prototypeA, Class<Map<String, String>> prototypeB) {
    //  super(prototypeA, prototypeB); this is not working
        
        super((Class<KeyValuePair[]>) (Class<?>) KeyValuePair[].class, (Class<Map<String, String>>) (Class<?>) HashMap.class); //no luck
    }

    @Override
    public Map<String, String> convertTo(KeyValuePair[] keyValuePairArray, Map<String, String> map) {
        System.out.println("***convertTo***");
        if(keyValuePairArray == null || keyValuePairArray.length == 0 )
            return new HashMap<>();
        
        map = new HashMap<>();
        for(KeyValuePair keyValuePair : keyValuePairArray) {
            map.put(keyValuePair.getKey(), keyValuePair.getValue());
        }
        
        return map;
    }

    @Override
    public KeyValuePair[] convertFrom(Map<String, String> map, KeyValuePair[] keyValuePair) {
        System.out.println("***convertFrom***");
        if (map == null || map.isEmpty())
            return new KeyValuePair[0];
        int count = 0;
        keyValuePair = new KeyValuePair[map.size()];
        for (Entry<String, String> entry : map.entrySet()) {
            if (entry.getValue() != null) {
                keyValuePair[count++] = new KeyValuePair(entry.getKey(), entry.getValue());
            }
        }
        return keyValuePair;
    }

    
}

Always, getting this exception:

Exception in thread "main" org.dozer.MappingException: java.lang.InstantiationException: com.dozer.test.MapToKeyValuePairArrayConverter
    at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:82)

My dozer bean mappings xml:

<configuration>
<custom-converters>
 <converter type="com.dozer.test.MapToKeyValuePairArrayConverter">
                <class-a>com.b2b.KeyValuePair[]</class-a>
                <class-b>java.util.HashMap</class-b>
 </converter>
</custom-converters>
</configuration>
    
    
<mapping>
  <class-a>com.api.EmployeeSource</class-a>
  <class-b>com.b2b.EmployeeTarget</class-b>    
   <field custom-converter="com.dozer.test.MapToKeyValuePairArrayConverter">
    <a>kvMap</a>
    <b>kvMap</b>
    <a-hint>java.util.HashMap</a-hint>  <!-- tried with and without hints but no luck -->
    <b-hint>com.b2b.KeyValuePair[]</b-hint>
   </field>
</mapping>
0

There are 0 best solutions below