有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Mapstruct:HashMap作为源到对象

如何使用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);
}

我的Util:

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

但它返回的属性和值都是空的。知道我做错了什么吗


共 (3) 个答案

  1. # 1 楼答案

    最好的方法是最简单的方法:

    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());
    }
    
  2. # 2 楼答案

    供将来参考。从Mapstruct 1.5开始,我认为这个用例是现成的。见mapstruct release note

  3. # 3 楼答案

    不确定你想要实现什么。如果映射更复杂,那么最好的方法就是使用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时,与其他制图