【问题标题】:JAXB type is already definedJAXB 类型已定义
【发布时间】:2016-11-03 15:57:08
【问题描述】:

我想从我的 Java 项目中调用 ISAN Restful API,所以我尝试使用 maven-jaxb2-plugin 从 xsd 文件生成 java bean。 这是xsds:

我下载了这些文件并将它们复制到我的 src/main/resources 文件夹中。 这是我在 pom.xml 中的插件配置:

        <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.12.3</version>
            <configuration>
                <schemaDirectory>src/main/resources</schemaDirectory>
            </configuration>

            <executions>
                <execution>
                    <id>isan</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatePackage>org.isan</generatePackage>
                        <generateDirectory>${project.build.directory}/generated-sources/isan</generateDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

现在插件告诉我某些类型被定义了多次。例如:

03/11/2016 16:42:53 UTC+1:[错误] 解析架构时出错。位置 [file:/D:/isan/isan/src/main/resources/common.xsd{ 51,37}]。 'DurationType' 已定义 2016 年 3 月 11 日 16:42:53 UTC+1:[错误] 解析架构时出错。位置 [http://www.isan.org/schema/v1.11/common/common.xsd{46,38}]。 (与上述错误有关)这里出现第一个定义

我理解这是因为每种类型都在我的本地文件和远程文件中定义。一种解决方案似乎是使用目录 (https://github.com/highsource/maven-jaxb2-plugin/wiki/Using-Catalogs),但它似乎不起作用。

我在“src/main/resources:”中添加了这个目录

PUBLIC "http://www.isan.org/schema/v1.11/common/common.xsd" "./common.xsd"
PUBLIC "http://www.isan.org/schema/v1.11/common/country.xsd" "./country.xsd"
PUBLIC "http://www.isan.org/schema/v1.11/common/externalid.xsd" "./externalid.xsd"
PUBLIC "http://www.isan.org/schema/v1.11/common/isan.xsd" "./isan.xsd"
PUBLIC "http://www.isan.org/schema/v1.11/common/language.xsd" "./language.xsd"
PUBLIC "http://www.isan.org/schema/v1.11/common/participant.xsd" "./participant.xsd"
PUBLIC "http://www.isan.org/schema/v1.11/common/serial.xsd" "./serial.xsd"
PUBLIC "http://www.isan.org/schema/v1.11/common/title.xsd" "./title.xsd"
PUBLIC "http://www.isan.org/schema/v1.11/common/version.xsd" "./version.xsd"

REWRITE_SYSTEM "http://www.isan.org" "isan"

并修改插件配置:

                <configuration>
                    <strict>false</strict>
                    <catalog>src/main/resources/catalog.cat</catalog>
                    <generatePackage>org.isan</generatePackage>
                    <generateDirectory>${project.build.directory}/generated-sources/isan</generateDirectory>
                </configuration>

我仍然遇到同样的错误

【问题讨论】:

    标签: java xsd jaxb


    【解决方案1】:

    你遇到了这个错误:

    https://github.com/javaee/jaxb-v2/issues/1045

    问题在于,当 XJC 使用目录文件时,它最终会将 已解析 URL 与命名空间相关联,而不是原始 URL。这会导致这个奇怪的错误,即同一类型被报告多次定义。

    解决方案是使用目录(与您一样),但编译绝对 URL 而不是本地文件。在这种情况下,您的所有架构都将具有原始 URL。

    这就是你的做法:

    创建架构文件的本地副本。我发现在服务器上复制目录结构更好。所以你会有类似的东西:

    isan
      schema
        v1.11
          common
            common.xsd
            version.xsd
            title.xsd
            externalid.xsd
            participant.xsd
            language.xsd
            country.xsd
        v1.21
          common
            serial.xsd
      ISAN
        isan.xsd
    

    写一个目录文件重写绝对URL:

    REWRITE_SYSTEM "http://www.isan.org" "isan"
    

    使用此目录并从绝对 URL 编译您的架构,例如:

    <strict>false</strict>
    <catalog>src/main/resources/catalog.cat</catalog>
    <schemas>
        <schema>
            <url>http://www.isan.org/schema/v1.11/common/common.xsd</url>
        </schema>
        <schema>
            <url>http://www.isan.org/schema/v1.21/common/serial.xsd</url>
        </schema>
        <!-- ... -->
    </schemas>
    

    您可能只需要几个 URL,因为架构可能会相互导入。

    当架构在xs:import 中同时使用相对 URL 和绝对 URL 时,您可能会遇到一个问题。相对 URL 将被解析为本地 URL,因此您会遇到同样的问题。在这种情况下,我必须修补模式以使它们都只使用绝对 URL。

    这里有几个项目可以检查这种设置:

    检查schemas 模块以获取修补/准备好的模式。其他模块从绝对 URL 编译模式,但使用目录将绝对 URL 重写为此 schemas 模块中的资源。

    这是目录文件:

    REWRITE_SYSTEM "http://www.w3.org" "maven:org.hisrc.w3c:w3c-schemas:jar::!/w3c"
    

    这是来自pom.xml 的其中一个模块的sn-p:

    <schemas>
        <schema>
            <url>http://www.w3.org/2007/06/wsdl/wsdl20.xsd</url>
        </schema>
        <schema>
            <url>http://www.w3.org/2007/06/wsdl/http.xsd</url>
        </schema>
        <schema>
            <url>http://www.w3.org/2007/06/wsdl/rpc.xsd</url>
        </schema>
        <schema>
            <url>http://www.w3.org/2007/06/wsdl/soap.xsd</url>
        </schema>
        <schema>
            <url>http://www.w3.org/2007/06/wsdl/wsdl20-extensions.xsd</url>
        </schema>
        <schema>
            <url>http://www.w3.org/2007/06/wsdl/wsdl20-instance.xsd</url>
        </schema>
    </schemas>
    

    【讨论】:

    • 谢谢,这个答案确实有帮助,但我面临另一个问题。我编辑了我的帖子。
    • @julaudo 新问题 - 新问题。
    • 对不起。我创建了一个新问题:stackoverflow.com/questions/40468120/…
    猜你喜欢
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多