gpt4 book ai didi

java - 映射结构 : Dealing with HashMap entries as source

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:21:42 26 4
gpt4 key购买 nike

这是我的源对象:

public class Record {

public final long captureTime;
public final String environnement;
public final String bundle;
public final String type;
public final String id;
public final Map<String,Object> meta;

}

这是我的目标对象:

public class MappedRecord {

public final long captureTime;
public final String environnement;
public final String bundle;
public final String type;
public final String id;
public final String ip;
public final String server;

}

我的映射器如下所示:

public interface RecordMapper {

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


@Mappings({
@Mapping(source = "captureTime", target = "captureTime"),
@Mapping(source = "environnement", target = "environnement"),
@Mapping(source = "bundle", target = "bundle"),
@Mapping(source = "type", target = "type"),
@Mapping(source = "id", target = "id"),
@Mapping(expression = "java((String) r.meta.get(\"ip\"))", target = "ip"),
@Mapping(expression = "java((String) r.meta.get(\"server\"))", target = "server"),
})

MappedRecord toMappedRecord(Record r);

}

目前它运行良好,但我想知道是否有更“优雅”的方式将 map 条目设置为源。因为有了这个我没有设法使用“qualifiedByName”属性添加转换函数,看起来它只能在指定“源”时工作。我误解了什么吗?

我尝试了以下方法但没有得到满意的结果:

  • 为我的 Record 类中的特定字段覆盖 getter
  • 添加具有“qualifiedByName”属性的转换函数。像这样的东西:

    @Named("metaGetter")
    default String dataGetter (String property) {
    return (String) r.meta.get(property);
    }

    但显然这不会编译,因为属性名称作为正确的来源无效。

感谢您的宝贵时间。

最佳答案

编写自己的限定符:

public class MappingUtil {
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Ip {
}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public static @interface Server {
}
@Ip
public String ip(Map<String, Object> in) {

return (String)in.get("ip");
}
@Server
public String server(Map<String, Object> in) {

return (String)in.get("server");
}
}

然后只需添加到映射中:

@Mapper( uses = MappingUtil.class )
public interface SourceTargetMapper {

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

@Mappings( {
@Mapping(source = "map", target = "ip", qualifiedBy = MappingUtil.Ip.class ),
@Mapping(source = "map", target = "server", qualifiedBy = MappingUtil.Server.class ),
} )
Target toTarget( Source s );
}

关于java - 映射结构 : Dealing with HashMap entries as source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44390808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com