【问题标题】:Mapstruct: HashMap as source to ObjectMapstruct:HashMap 作为 Object 的源
【发布时间】:2019-02-08 22:13:18
【问题描述】:

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

这是我的目标对象:

public class ComponentStyleDTO{
    private String attribute;
    private Object value;
}

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

我的映射器:

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

我的实用程序:

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

    }

}

这就是我的使用方法:

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

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

【问题讨论】:

  • 你在 DTO 上有 getter/setter(或 lombok 左右)吗?

标签: java spring mapstruct


【解决方案1】:

供日后参考。从 Mapstruct 1.5 开始,我认为这个用例是开箱即用的。见mapstruct release note

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

不确定您要达到的目标。如果您的映射更复杂,也许最好的方法确实是使用https://stackoverflow.com/a/54601058/1115491 中的方法。

另一方面,它不适合您的原因是您没有为映射定义源。在您链接的示例中,有一个 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);
}

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

【讨论】:

  • 谢谢。我确实使用了另一种方法,但感谢您对此进行了回复!
【解决方案3】:

恕我直言,最好的方法是最简单的方法:

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

default List<ComponentStyleDTO> hashMapToComponentStyleDTO(HashMap<String, Object> hashMap){
    return hashMap.entrySet()
                  .stream()
                  .map(e -> new ComponentStyleDTO(e.getKey(), e.getValue()))
                  .collect(Collectors.toList());
}

【讨论】:

  • 谢谢,但这在我的示例中不起作用,因为该值可以是另一个哈希图。
猜你喜欢
  • 1970-01-01
  • 2014-07-05
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 1970-01-01
  • 2019-02-24
  • 2015-08-14
相关资源
最近更新 更多