Mapstruct:HashMap 作为对象的源

dav*_*vis 6 java spring mapstruct

我如何使用 aHashMap<String, Object>作为对象的源?

这是我的目标对象:

public class ComponentStyleDTO{
    private String attribute;
    private Object value;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用我发现的这种方法,这也在文档中,但对我来说失败了。

我的映射器:

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, componentModel = "spring", uses = ComponentStyleMapperUtil.class)
public interface ComponentStyleMapper {

    ComponentStyleMapper MAPPER = Mappers.getMapper(ComponentStyleMapper.class);

    @Mappings({@Mapping(target = "attribute", qualifiedBy = ComponentStyleMapperUtil.Attribute.class),
    @Mapping(target = "value", qualifiedBy = ComponentStyleMapperUtil.Value.class)})
    ComponentStyleDTO hashMapToComponentStyleDTO(HashMap<String, Object> hashMap);
}
Run Code Online (Sandbox Code Playgroud)

我的实用程序:

public class ComponentStyleMapperUtil{
    @Qualifier
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.SOURCE)
    public @interface Attribute {
    }

    @Qualifier
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.SOURCE)
    public @interface Value {
    }


    @Attribute
    public String attribute(HashMap<String, Object> in){

        return (String) in.entrySet().stream().findFirst().get().getKey();
    }

    @Value
    public Object value(HashMap<String, Object> in) {
        Object value = in.entrySet().stream().findFirst().get().getValue();

        if(value instanceof String){
            return value;
        }else if(value instanceof LinkedHashMap){
            List<ComponentStyleDTO> childs = new ArrayList<ComponentStyleDTO>();
            HashMap<String, Object> child = (HashMap<String, Object>) value;
            for(String key: child.keySet()){
                ComponentStyleDTO schild = new ComponentStyleDTO();
                schild.setAttribute(key);
                schild.setValue((String) child.get(key));
                childs.add(schild);
            }
            return childs;
        }else{
            return value;
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

这是我如何使用它:

    HashMap<String, Object> hmap = new HashMap<String, Object>();
    hmap.put(attr.getKey(), attr.getValue());
    ComponentStyleDTO componentDTO = componentStyleMapper.hashMapToComponentStyleDTO(hmap);
Run Code Online (Sandbox Code Playgroud)

但它让我在属性和值上都是空的。知道我可能做错了什么吗?

Fil*_*lip 3

不确定您想要实现什么目标。如果您的映射更复杂,也许最好的方法确实是采用/sf/answers/3822074091/中的方法。

另一方面,它对您不起作用的原因是您尚未定义映射的源。在您链接的示例中,有一个 POJO 作为源参数,源是该 POJO 中的映射。为了使其工作,您的映射器需要如下所示:

@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);
}
Run Code Online (Sandbox Code Playgroud)

注意:使用非默认值时,componentModel不应使用Mappers工厂来获取映射器的实例。如果您这样做,您在与其他映射器一起工作时将获得 NPE。