invaliddefinitionexception:无法构造“javax.xml.bind.jaxbelement”的示例(不存在像default construct这样的创建者)

nr7wwzry  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(1351)

我在读取xml文件cobieliteexample.xml时遇到问题。我使用带有maven插件的多个xsd文件cobielite.xsd、core.xsd生成了xml绑定。我在github上推了一个问题的最小可再现性示例:https://github.com/simiii/xsd-xml-mess
生成xml绑定类的maven插件:

<plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>
                    <execution>
                        <id>cobieLiteRc4</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <forceRegenerate>true</forceRegenerate>
                            <generateDirectory>${project.build.directory}/generated-sources/cobielite
                            </generateDirectory>
                            <schemaDirectory>${project.basedir}/src/main/resources/cobielite/rc4</schemaDirectory>
                            <schemaIncludes>
                                <inclucde>cobielite.xsd</inclucde>
                            </schemaIncludes>
                            <bindingDirectory>
                                ${project.basedir}/src/main/resources/cobielite/rc4
                            </bindingDirectory>
                            <addCompileSourceRoot>true</addCompileSourceRoot>
                            <strict>false</strict>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

正在读取xml文件:

final XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.registerModule(new JaxbAnnotationModule());
        xmlMapper.setDefaultUseWrapper(true);
        xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
        xmlMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        try (InputStream is = new FileInputStream(new File("cobieLiteExample.xml"))) {
            final FacilityType facilityType = xmlMapper.readValue(is, new TypeReference<FacilityType>() {
            });
        }

异常日志:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `javax.xml.bind.JAXBElement` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('First Floor')
 at [Source: (FileInputStream); line: 21, column: 29] (through reference chain: org.buildingsmartalliance.docs.nbims03.cobie.cobielite.FacilityType["Floors"]->org.buildingsmartalliance.docs.nbims03.cobie.core.FloorCollectionType["Floor"]->java.util.ArrayList[0])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
    at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1452)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1028)
    at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371)
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1373)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:171)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:286)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:136)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:136)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3077)
    at org.example.Main.main(Main.java:27)

更新
问题是jaxb生成了jaxbelement Package 类,在使用xmlmapper类时,这些类对我来说似乎是不可解析的。
同样的问题:
jaxb避免jaxbelement<?>
为什么jaxb有时Map到jaxbelement?
现在我想到了两个可能的解决办法:
告诉jaxb不要生成jaxbelement Package 器类->这可以通过删除substitutiongroup来实现,这将修改模型,并导致生成的java类的不可用、不完整的信息。
修改xmlmapper示例以处理jaxbelement Package 器。
变通办法
作为解决方法,我更改了如何读取xml文件的实现:

JAXBContext jc = JAXBContext.newInstance("org.buildingsmartalliance.docs.nbims03.cobie.cobielite");
Unmarshaller u = jc.createUnmarshaller();

final JAXBElement<FacilityType> facilityTypeJAXBElement = (JAXBElement<FacilityType>) u.unmarshal(is);
final FacilityType facilityType = facilityTypeJAXBElement.getValue();

任何帮助都将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题