Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何用Orika映射泛型对象?_Java_Generics_Converter_Orika - Fatal编程技术网

Java 如何用Orika映射泛型对象?

Java 如何用Orika映射泛型对象?,java,generics,converter,orika,Java,Generics,Converter,Orika,我正在使用Orika 1.4.5,我想让我的双向转换器映射 PaginatedResponse到PaginatedResponse 维切维萨 PaginatedResponse类如下所示: public class PaginatedResponse<T> { private List<T> items; private List<Sorting> orderBy; private PagingResponse paging;

我正在使用Orika 1.4.5,我想让我的双向转换器映射
PaginatedResponse到PaginatedResponse
维切维萨

PaginatedResponse类如下所示:

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
}
公共类分页响应{
私人清单项目;
私有列表排序依据;
专用寻呼应答寻呼;
公共分页响应(列表项、列表排序依据、分页响应分页){
这个项目=项目;
this.orderBy=orderBy;
this.paging=分页;
}
//吸气剂
}
因此,我希望我的PaginatedResponseCovner接受所有映射调用,其中转换为
PaginatedResponse object1到PaginatedResponse object2
,并且我希望object1和object2具有相同的orderBy和分页属性。所以我试着这样做:

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
    }
}
公共类分页响应转换器
扩展双向转换器
实现MapperAware{
私有映射器;
私人T类1;
二等兵;
公共分页响应转换器(T类1,S类2){
this.clazz1=clazz1;
this.clazz=clazz;
}
@凌驾
公共无效设置映射器(映射器映射器){
this.mapper=(MapperFacade)映射器;
}
@凌驾
@抑制警告(“未选中”)
公共分页响应转换器(分页响应源,类型destinationType){
System.out.println(“ConverTo”);
PaginResponse paging=source.getPaging();
List sortings=source.getOrderBy();
List items=(List)this.mapper.mapAsList(source.getItems(),this.clazz2.getClass());
返回新的分页响应(项目、排序、分页);
}
@凌驾
@抑制警告(“未选中”)
公共分页响应转换源(分页响应源,类型destinationType){
//同样的颠倒
}
}
但问题是,我必须用泛型参数注册这个自定义转换器,而这些参数并不总是相同的。我希望如果我尝试从
PaginatedResponse转换为PaginatedResponse
PaginatedResponse转换为PaginatedResponse
相同,顺便说一句,我不能这样做:

converterFactory.registerConverter(new PaginatedResponseConverter<Object, Object>(Object.class, Object.class));
converterFactory.registerConverter(新的分页响应转换程序(Object.class,Object.class));
因为通过这种方式,所有PaginatedResponse调用都将进入PaginatedResponseConverter,但我不知道类的真正类型, 因此,当它进入converTo或convertFrom方法时,需要泛型参数的确切类来执行mapAsList()方法


你能帮我解决这个问题吗?

有一个基于Orika的
TypeBuilder
的解决方案,它创建
Type
-s来存储它们的类型参数

我的代码将把
PaginatedResponse
映射到
PaginatedResponse
,然后映射到
PaginatedResponse
。如果您不介意一些一般性警告,下面是:

驾驶员等级:

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

我担心这是不可能的,因为在反射(Orika将使用反射)中,不可能检索分配给通用占位符的类;这完全是编译器的事情。谢谢你的回复。你对如何解决这个问题有什么想法吗?我的意思是,也许我应该使用映射器而不是转换器。你认为这会改变什么吗?当做您必须以其他方式传输此信息,可能是通过另一个包含泛型参数的clas或类名的字段。无论使用哪种工具,反射的一般问题都是一样的。
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);
    }
}