java - JAXB 覆盖 package-info.java : What should be "namespace"?

标签 java xml jaxb

我们使用 xjc 生成用于 XML 生成的 JAXB Java 类。一切正常,除了我们尝试调整生成的命名空间前缀 as described here 。由于我们使用的 JAXB 版本,我们陷入了“解决方案 2”,调整 package-info.java。

我们的结构是多个深度导入:根命名空间导入其他命名空间,而其他命名空间又导入第三个命名空间。

MCVE xsd

root.xsd(导入 other.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="root" xmlns:other="other" targetNamespace="root" version="1.0">
    <xs:import namespace="other" schemaLocation="other.xsd" />
    <xs:element name="rootElem">
        <xs:complexType>
            <xs:choice>
                <xs:element ref="rootElem1"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    <xs:element name="rootElem1" nillable="false">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="data">
                    <xs:complexType>
                        <xs:choice>
                            <xs:element ref="other:otherElem"/>
                        </xs:choice>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    </xs:schema>

other.xsd(导入third.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:other="other" xmlns:third="third" targetNamespace="other" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">
<xsd:import namespace="third" schemaLocation="third.xsd" />
    <xsd:element name="otherElem">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="third:thirdElem" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

第三个.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:third="third" targetNamespace="third" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">
    <xsd:element name="thirdElem">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="thirdData" type="xsd:string" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

有了这个

简单测试用例

@Test
public void test() throws JAXBException
{
    Marshaller marshaller = JAXBContext.newInstance("test.jaxb.generated").createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.com/schema.xsd");

    RootElem root = new RootElem();
    RootElem1 root1 = new RootElem1();
    Data d = new Data();
    OtherElem other = new OtherElem();
    ThirdElem thirdElem = new ThirdElem();
    thirdElem.setThirdData("third");
    other.setThirdElem(thirdElem);
    d.setOtherElem(other);
    root1.setData(d);
    root.setRootElem1(root1);

    Path path = Paths.get("target", "outfile.xml");

    Result result = new StreamResult(path.toFile());
    marshaller.marshal(root, result);
}

结果是这样的

生成的 XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:rootElem xmlns:ns2="other" xmlns:ns3="third" xmlns:ns4="root" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/schema.xsd">
    <ns4:rootElem1>
        <data>
            <ns2:otherElem>
                <ns3:thirdElem>
                    <ns3:thirdData>third</ns3:thirdData>
                </ns3:thirdElem>
            </ns2:otherElem>
        </data>
    </ns4:rootElem1>
</ns4:rootElem>

一切都很好(除了 data ,它没有任何关联的命名空间,我假设因为这是一个内部类型)。

现在这部分是问题:这是

生成的package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "other", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package test.jaxb.generated;

为什么命名空间引用其他?我的根命名空间是root。 (root.xsd 是我们为 Maven jaxb2-maven-plugin 提供的唯一文件;我们可以包含其他文件,这没有区别)。

错误替换package-info.java

如果我们用以下内容覆盖生成的:

@javax.xml.bind.annotation.XmlSchema(
        namespace = "root",
        xmlns = {
                @javax.xml.bind.annotation.XmlNs(prefix = "t", namespaceURI = "third"),
                @javax.xml.bind.annotation.XmlNs(prefix = "o", namespaceURI = "other"),
                @javax.xml.bind.annotation.XmlNs(prefix = "r", namespaceURI = "root")
        },
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package test.jaxb.generated;

我们最初这样做是因为我们假设我们必须在这里给出根命名空间 - 这就是

生成的 XML 错误

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<r:rootElem xmlns:t="third" xmlns:o="other" xmlns:r="root" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/schema.xsd">
    <r:rootElem1>
        <data>
            <r:otherElem>
                <t:thirdElem>
                    <t:thirdData>third</t:thirdData>
                </t:thirdElem>
            </r:otherElem>
        </data>
    </r:rootElem1>
</r:rootElem>

现在命名空间很漂亮,但错误! otherElem 属于 o,而不是 r

将覆盖文件中的命名空间更改回 other 可以修复错误,但再次:

问题是为什么必需的命名空间 other 在这里?同样令人困惑的是,第三导入的图层无论哪种方式都是正确的。

问题已解决,但我们想了解这个概念。我们还缺少什么?

编辑:

为了完整起见,这是我的 pom.xmlbuild 部分:

<build>
    <plugins>
        <plugin>
        <!-- run xjc -->
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>xjc-generate_classes</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <packageName>test.jaxb.generated</packageName>
                        <schemaFiles>
                            root.xsd
                        </schemaFiles>
                        <schemaDirectory>${basedir}/src/main/resources/schemata</schemaDirectory>
                        <extension>true</extension>
                        <bindingDirectory>${basedir}/src/main/resources/bindings</bindingDirectory>
                        <outputDirectory>${basedir}/target/generated-sources/jaxb/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- overwrite created package-info.java -->
         <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>true</overwrite>
                        <outputDirectory>${basedir}/target/generated-sources/jaxb/test/jaxb/generated</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/resources/bindings</directory>
                                <include>package-info.java</include>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

最佳答案

问题是您在一个包中生成所有类(使用 Maven 插件的 packageName 配置。

不要这样做。

JAXB 在某种程度上基于包命名空间对应的概念。您使用的每个命名空间应该有一个包。虽然技术上可以采取其他方式,但您将面临一个又一个问题。因此,最好遵循这一概念,并为每个命名空间使用或生成一个包。您仍然可以配置目标包 - 但使用绑定(bind)文件而不是插件配置元素。

关于java - JAXB 覆盖 package-info.java : What should be "namespace"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50742511/

相关文章:

java - 安卓 : Hint-text color not changing even after manually setting in XML and/or Java

java - 使用 JAXB 解码 XOP

java - Seam Mail 内联图像问题

java - 使用私有(private)构造函数扩展类

java - HttpClient 的多线程问题

java - 使用 DOM 迭代包含一些具有相同名称的标签的 NodeList

Java - 如何将 HS256 与 JwtAccessTokenConverter 一起使用

xml - XPath 3.0将日期差异与持续时间文字进行比较

jaxb - Eclipselink 和 @XmlRef 的问题

java - 如何使用 jvnet maven-jaxb2-plugin 从类生成 XSD