Mapstruct: HashMap as source to Object
IMHO The best way is the simpliest way:
default ComponentStyleDTO hashMapToComponentStyleDTO(HashMap<String, Object> hashMap){
ComponentStyleDTO result = new ComponentStyleDTO();
result.setAtribute1(hashMap.get("atribute1"));
result.setAtribute2(hashMap.get("atribute2"));
result.setAtribute3(hashMap.get("atribute3"));
...
return result;
}
or
default List<ComponentStyleDTO> hashMapToComponentStyleDTO(HashMap<String, Object> hashMap){
return hashMap.entrySet()
.stream()
.map(e -> new ComponentStyleDTO(e.getKey(), e.getValue()))
.collect(Collectors.toList());
}
Not sure what you are trying to achieve. If your mapping is more complex maybe the best way is indeed to go with the approach in https://stackoverflow.com/a/54601058/1115491.
That a side, the reason why it is not working for you is that you have not defined the source for your mapping. In the example you linked there is a POJO as source parameter and the source is a map in that POJO. In order to make it work your mapper needs to look like:
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, componentModel = "spring", uses = ComponentStyleMapperUtil.class)
public interface ComponentStyleMapper {
@Mapping(target = "attribute", source = "hashMap", qualifiedBy = ComponentStyleMapperUtil.Attribute.class)
@Mapping(target = "value", source = "hashMap", qualifiedBy = ComponentStyleMapperUtil.Value.class)
ComponentStyleDTO hashMapToComponentStyleDTO(HashMap<String, Object> hashMap);
}
NB: When using the non default componentModel
you should not use the Mappers
factory for getting an instance of the mapper. If you do that you will get an NPE when working with other mappers.