繁体   English   中英

Orika通用集合自定义映射

[英]Orika Generic Collection Custom Mapping

Orika支持泛型类型,但是我很难使其与泛型集合一起使用。 由于Orika不支持不同的收集策略(累积,非累积,孤立删除),因此我需要编写一个自定义映射器来满足我的要求。

问题在于Orika没有应用此映射器,而是尝试使用常规的集合映射逻辑。

Type<List<Document>> DOCUMENT_LIST = new TypeBuilder<List<Document>>() {}.build();
Type<List<DocumentRepresentation>> DOCUMENT_REP_LIST = new TypeBuilder<List<DocumentRepresentation>>() {}.build();

mapperFactory.classMap(DOCUMENT_LIST, DOCUMENT_REP_LIST)
                .mapNulls(true)
                .mapNullsInReverse(true)
                .customize(new NonCumulativeListMapperDocumentToDocumentRepresentation())
                .register();

public class NonCumulativeListMapperDocumentToDocumentRepresentation
        extends CustomMapper<List<Document>, List<DocumentRepresentation>> {
    //mapping logic
}

我还尝试在父映射中显式设置类型列表

.fieldMap("documents", "documents")
.aElementType(Document.class)
.bElementType(DocumentRepresentation.class)
.add()

但这也没有得到解决。

关于我所缺少的任何提示吗?

这可以通过注册您的自定义映射器来完成:

mapperFactory.registerMapper(new NonCumulativeListMapperDocumentToDocumentRepresentation());

当Orika必须映射DOCUMENT_LIST DOCUMENT_REP_LIST时,它将在以后使用。 不需要最后的fieldMap配置。

有关在Orika中合并集合的更多信息,请参考此简单测试(CustomMergerTest)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM