Orika映射到JAXBElement<>

时间:2016-12-19 04:32:12

标签: java jaxb orika data-mapping

我正在使用JAXB从XSD生成POJO。由于某些原因,生成的POJO具有JAXBElement<CustomObject>作为属性。

public class ResponsePackageType{

   protected JAXBElement<? extends ResponseType> response;
   ....
}

多个类正在扩展ResponseType,但我只想映射其中一种类型,而Orika在识别此属性时遇到问题。

我的代码:

JAXBElement<ResponsePackageType> feed = (JAXBElement<ResponsePackageType>) unmarshaller.unmarshal(new FileInputStream( "input.xml" ));

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

Type<JAXBElement<ResponsePackageType>> respPackageType = new TypeBuilder<JAXBElement<ResponsePackageType>>(){}.build();

Type<JAXBElement<SpecialResponseType>> spRespType = new TypeBuilder<JAXBElement<SpecialResponseType>>(){}.build();

mapperFactory.classMap(respPackageType, respPackageType)
        .fieldMap("response", "response")
        .aElementType(spRespType )
        .bElementType(spRespType )
        .add()
        .byDefault().register();

MapperFacade mapper = mapperFactory.getMapperFacade();

mapper.map(feed, objectFactory.createResponsePackage(null));

例外:

Exception in thread "main" java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    at ma.glasnost.orika.constructor.ConstructorParameterResolver.getProperties(ConstructorParameterResolver.java:73)
    at ma.glasnost.orika.constructor.ConstructorParameterResolver.getPossibleConstructorParams(ConstructorParameterResolver.java:119)
    at ma.glasnost.orika.property.PropertyResolver.resolveConstructorProperty(PropertyResolver.java:701)
    at ma.glasnost.orika.property.PropertyResolver.getProperty(PropertyResolver.java:686)
    at ma.glasnost.orika.property.PropertyResolver.getProperty(PropertyResolver.java:616)
    at ma.glasnost.orika.metadata.ClassMapBuilder.resolveProperty(ClassMapBuilder.java:828)
    at ma.glasnost.orika.metadata.ClassMapBuilder.resolvePropertyForA(ClassMapBuilder.java:838)
    at ma.glasnost.orika.metadata.ClassMapBuilder.fieldMap(ClassMapBuilder.java:379)

我该如何解决这个问题?我应该使用propertyResolverStrategy吗?

0 个答案:

没有答案