eclipselink - 动态 JAXB 异常

标签 eclipselink moxy

我正在使用 eclipse link(v2.5.0) Dynamic JAXB 使用多个模式将 XML 转换为 JSON,反之亦然。

emp.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:emp="Employee:2:0" targetNamespace="Employee:2:0"
    elementFormDefault="unqualified" attributeFormDefault="unqualified"
    version="2.0">

    <xsd:element name="searchManager" type="emp:SearchManager" />

    <xsd:complexType name="SearchManager">
        <xsd:sequence>
            <xsd:element name="CompanyName" type="xsd:string" />
            <xsd:element name="objects" type="emp:Employee" minOccurs="0" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>


    <xsd:complexType name="Employee">

        <xsd:complexContent>
            <xsd:extension base="emp:Organization">
                <xsd:sequence>
                   <xsd:element name="EmpId" type="xsd:string" minOccurs="0" />
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>

    <xsd:complexType name="Projects">
        <xsd:complexContent>
            <xsd:extension base="emp:Organization"/>
        </xsd:complexContent>
    </xsd:complexType>

    <xsd:complexType name="Organization">
        <xsd:annotation>
            <xsd:documentation>Abstract base class </xsd:documentation>
        </xsd:annotation>
    </xsd:complexType>
</xsd:schema>

管理器.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema targetNamespace="Manager:1:0" xmlns:emp="Employee:2:0"
    xmlns:manager="Manager:1:0" xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="unqualified" attributeFormDefault="unqualified"
    version="1.0">
    <!-- schema imports -->
    <xs:import namespace="Employee:2:0" schemaLocation="emp.xsd" />

    <xs:complexType name="Manager">
        <xs:annotation>
            <xs:documentation>
                Definition of class Employee
            </xs:documentation>
        </xs:annotation>
        <xs:complexContent>
            <xs:extension base="emp:Employee">
                <xs:sequence>
                    <xs:element name="teamSize" type="xsd:int" minOccurs="0" />
                    <xs:element name="project1" type="manager:Project1"
                        minOccurs="0" maxOccurs="unbounded" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>


    <xs:complexType name="Project1">
        <xs:complexContent>
            <xs:extension base="manager:Developement">
                <xs:sequence>
                    <xs:element name="type" type="xsd:int" minOccurs="0" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="Developement">
        <xs:annotation>
            <xs:documentation>
                Abstract base class for an Development
            </xs:documentation>
        </xs:annotation>
        <xs:complexContent>
            <xs:extension base="emp:Projects"/>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

示例.xml

<emp:searchManager xmlns:emp="Employee:2:0"
    xmlns:manager="Manager:1:0">
    <CompanyName>Test</CompanyName>
    <objects xmlns:ns2="Manager:1:0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:Manager">
        <EmpId>123456</EmpId>
        <teamSize>10</teamSize>
        <project1>
            <type>1</type>
        </project1>
    </objects>
</emp:searchManager>

示例驱动程序

public class XMLToJSON {

    /**
     * @param args
     */
    public static void main(String[] args) {

        FileInputStream xsdInputStream;
        try {
            xsdInputStream = new FileInputStream("Manager.xsd");
            DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, new MyEntityResolver(), null, null);
            FileInputStream xmlInputStream = new FileInputStream("sample.xml");
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            JAXBElement<DynamicEntity> manager = (JAXBElement) unmarshaller.unmarshal(xmlInputStream);


            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //input xml to print
            marshaller.marshal(manager, System.out);

            Map namespaces = new HashMap();
            namespaces.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
            namespaces.put("Employee:2:0", "ns1"); 
            namespaces.put("Manager:1:0", "ns2");

            // XML to JSON
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
            marshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
            FileOutputStream jsonOutputStream = new FileOutputStream("sample.json");
            marshaller.marshal(manager, jsonOutputStream);
            marshaller.marshal(manager, System.out);

            //JSON to XML
            JAXBUnmarshaller jsonUnmarshaller = jaxbContext.createUnmarshaller();
            jsonUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
            jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespaces);
            jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
            StreamSource json = new StreamSource("sample.json");
            JAXBElement<DynamicEntity> myroot = (JAXBElement) jsonUnmarshaller.unmarshal(json);
            Marshaller m = jaxbContext.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
            m.marshal(myroot, System.out);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

实体解析器

class MyEntityResolver implements EntityResolver {

       public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
          String filename = new File(systemId).getName();

          // Now prepend the correct path

          InputSource is = new InputSource(ClassLoader.getSystemResourceAsStream(filename));
          is.setSystemId(filename);

          return is;
       }

    }

生成的 JSON

{
   "searchManager" : {
      "CompanyName" : "Test",
      "objects" : [ {
         "type" : "Manager",
         "EmpId" : "123456",
         "teamSize" : 10,
         "project1" : [ {
            "type" : 1
         } ]
      } ]
   }
}

解码时出现以下异常

Exception in thread "main" java.lang.NullPointerException
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:264)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:443)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:374)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:241)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:443)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:374)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:241)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:443)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:296)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parseRoot(JSONReader.java:166)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:125)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:140)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:778)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:666)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:593)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:287)
    at XMLToJSON.main(XMLToJSON.java:65)

如果在示例驱动程序中禁用命名空间,则会看到以下异常

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-43] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class for indicator field value [1] of type [class java.lang.String].
Descriptor: XMLDescriptor(manager._1._0.Project1 --> [])
    at org.eclipse.persistence.exceptions.DescriptorException.missingClassForIndicatorFieldValue(DescriptorException.java:938)
    at org.eclipse.persistence.internal.oxm.QNameInheritancePolicy.classFromRow(QNameInheritancePolicy.java:264)
    at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.classFromRow(TreeObjectBuilder.java:182)
    at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.classFromRow(TreeObjectBuilder.java:1)
    at org.eclipse.persistence.internal.oxm.XMLRelationshipMappingNodeValue.processChild(XMLRelationshipMappingNodeValue.java:63)
    at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.startElement(XMLCompositeCollectionMappingNodeValue.java:184)
    at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.startElement(UnmarshalRecordImpl.java:834)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:372)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:241)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:443)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:374)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:241)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:443)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:296)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parseRoot(JSONReader.java:166)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:125)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:140)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:778)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:666)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:593)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:287)
    at XMLToJSON.main(XMLToJSON.java:65)

这是 Dynamic JAXB Moxy 中的错误吗?在不更改架构或 xml 文件的情况下是否有任何解决方法?

最佳答案

您遇到了 2.5.0 bug在我们刚刚在 EclipseLink 2.5.1 和 2.6.0 流中修复的 JSON 解码代码中。从2013 年 8 月 8 日开始,您可以从以下链接下载包含此修复程序的夜间构建:http://www.eclipse.org/eclipselink/downloads/nightly.php .

关于eclipselink - 动态 JAXB 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102040/

相关文章:

java - eclipse java.lang.NoClassDefFoundError 中的 Hadoop

java - JPA多对多持久连接表

eclipselink - 如何在 WebLogic Server 12c 中用 EclipseLink 2.5 替换 EclipseLink 2.3.2

java - 通过 @XmlPath MOXy 注释解码嵌套集合

java - 如何使用导入另一个模式文件的模式文件验证 XML?

java - 删除 Derby 条目

java - 用于一对多关系的 JPA IN 子句

java - 如何使用 moxy 将 xsd 编译成一组等效的 java 对象?

java - 由于包结构导致的 EclipseLink MOXy 异常

java - 在访问器(getter)方法上使用转换器时出现异常