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中合并集合的更多信息,请参考此
simple test (CustomMergerTest)

07-24 09:38
查看更多