本文介绍了如何使用Orika映射泛型对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Orika 1.4.5,我想将我的BidirectionalConverter映射到PaginatedResponse<T> to PaginatedResponse<S>反之亦然.

I am using Orika 1.4.5, and i want to make my BidirectionalConverter to map PaginatedResponse<T> to PaginatedResponse<S> and viceversa.

PaginatedResponse类如下:

The PaginatedResponse class is as follows:

public class PaginatedResponse<T> {

    private List<T> items;
    private List<Sorting> orderBy;
    private PagingResponse paging;

    public PaginatedResponse(List<T> items, List<Sorting> orderBy, PagingResponse paging) {
        this.items = items;
        this.orderBy = orderBy;
        this.paging = paging;
    }

    // Getters
}

所以我希望我的PaginatedResponseCovnerter进行转换为PaginatedResponse<Something> object1 to PaginatedResponse<OtherSomething> object2的所有映射调用,并且我希望object1和object2具有相同的orderBy和分页属性.所以我尝试这样做:

So i want that my PaginatedResponseCovnerter takes all map calls where conversion is PaginatedResponse<Something> object1 to PaginatedResponse<OtherSomething> object2, and i want that object1 and object2 have same orderBy and paging attributes. So i try doings this:

public class PaginatedResponseConverter<T, S>
    extends BidirectionalConverter<PaginatedResponse<T>, PaginatedResponse<S>>
    implements MapperAware {

    private MapperFacade mapper;
    private T clazz1;
    private S clazz2;

    public PaginatedResponseConverter(T clazz1, S clazz2) {
        this.clazz1 = clazz1;
        this.clazz2 = clazz2;
    }

    @Override
    public void setMapper(Mapper mapper) {
        this.mapper = (MapperFacade) mapper;
    }


    @Override
    @SuppressWarnings("unchecked")
    public PaginatedResponse<S> convertTo(PaginatedResponse<T> source, Type<PaginatedResponse<S>> destinationType) {
        System.out.println("ConverTo");
        PagingResponse paging = source.getPaging();
        List<Sorting> sortings = source.getOrderBy();
        List<S> items = (List<S>) this.mapper.mapAsList(source.getItems(), this.clazz2.getClass());
        return new PaginatedResponse<S>(items, sortings, paging);
    }


    @Override
    @SuppressWarnings("unchecked")
    public PaginatedResponse<T> convertFrom(PaginatedResponse<S> source, Type<PaginatedResponse<T>> destinationType) {
        // The same upside down
    }
}

但是问题是我必须使用泛型参数注册此自定义转换器,并且它们并不总是相同的.我希望如果我尝试从PaginatedResponse<SomeClass1> to PaginatedResponse<SomeClass2>转换为与PaginatedResponse<AnotherClass1> to PaginatedResponse<AnotherClass2>相同,并且顺便说一句,我无法做到这一点:

But the problem with this is that i have to register this custom converter with generics arguments , and these are not always the same. I want that if i try to convert from PaginatedResponse<SomeClass1> to PaginatedResponse<SomeClass2> be the same that PaginatedResponse<AnotherClass1> to PaginatedResponse<AnotherClass2>, and by the way, i cannot do this:

converterFactory.registerConverter(new PaginatedResponseConverter<Object, Object>(Object.class, Object.class));

因为通过这种方式,所有PaginatedResponse调用都将进入PaginatedResponseConverter,但是我不知道类的真实类型,因此,当它进入converTo或convertFrom方法内部时,需要使用通用参数的确切类来执行mapAsList()方法

because by this way all PaginatedResponse calls will get into PaginatedResponseConverter but i dont know the real type from the classes, so when it goes inside the converTo or convertFrom method, that need the exact class of the generic argument to do the mapAsList() method

您能帮我吗?

推荐答案

此解决方案基于Orika的TypeBuilder,可创建存储其类型参数的Type -s.

There is this solution based on Orika's TypeBuilder that creates Type-s that store their type arguments.

我的代码会将PaginatedResponse<Source>映射到PaginatedResponse<Dest1>,然后再映射到PaginatedResponse<Dest2>.如果您不介意一些一般性警告,请按以下步骤进行:

My code will map a PaginatedResponse<Source> to a PaginatedResponse<Dest1> and then to a PaginatedResponse<Dest2>. If you don't mind some generic warnings, here it goes:

驱动程序类:

public static void main(String[] args) {
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
    ConverterFactory converterFactory = mapperFactory.getConverterFactory();
    converterFactory.registerConverter(new PaginatedToPaginatedConverter());

    PaginatedResponse<Source> sourcePaginatedResponse = createSourceObject();
    MapperFacade mapper = mapperFactory.getMapperFacade();

    PaginatedResponse dest1PaginatedResponse = mapper.map(sourcePaginatedResponse,
            new TypeBuilder<PaginatedResponse<Source>>(){}.build(),
            new TypeBuilder<PaginatedResponse<Dest1>>(){}.build());

    PaginatedResponse dest2PaginatedResponse = mapper.map(sourcePaginatedResponse,
            new TypeBuilder<PaginatedResponse<Source>>(){}.build(),
            new TypeBuilder<PaginatedResponse<Dest2>>(){}.build());
}

转换器,请注意,我没有费心去生成PaginatedResponse,而是改写了canConvert:

Converter, observe that I did not bother to generify PaginatedResponse, instead I overrode canConvert:

public class PaginatedToPaginatedConverter extends BidirectionalConverter<PaginatedResponse, PaginatedResponse> {

    public PaginatedResponse convertTo(PaginatedResponse paginatedResponse, Type<PaginatedResponse> destinationType) {
        PaginatedResponse dest = new PaginatedResponse();
        dest.setItems(mapperFacade.mapAsList(paginatedResponse.getItems(),
                ((Type) destinationType.getActualTypeArguments()[0]).getRawType()));
        dest.setOrderBy(mapperFacade.mapAsList(paginatedResponse.getOrderBy(), Sorting.class));
        dest.setPaging(mapperFacade.map(paginatedResponse.getPaging(), PagingResponse.class));
        return dest;
    }

    public PaginatedResponse convertFrom(PaginatedResponse paginatedResponse, Type<PaginatedResponse> destinationType) {
        return null;
    }

    @Override
    public boolean canConvert(Type<?> sourceType, Type<?> destinationType) {
        return sourceType.getRawType().equals(PaginatedResponse.class)
                && destinationType.getRawType().equals(PaginatedResponse.class);
    }
}

这篇关于如何使用Orika映射泛型对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:13