You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用jaxb2-maven-plugin 3.1.0处理旧XSD文件的编组错误

问题描述

Java 8环境下使用jaxb2-maven-plugin 2.5.0通过XSD生成DTO正常,迁移到Java 11后升级至jaxb2-maven-plugin 3.1.0,执行时出现错误:

org.xml.sax.SAXParseException: not an external binding file. The root element must be {https://jakarta.ee/xml/ns/jaxb}bindings but it is {http://www.w3.org/2001/XMLSchema}schema

使用的XSD文件开头内容如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="https://www.xxxxx.es/xxxxx/ApiEstandar"
    targetNamespace="https://www.xxxxx.es/xxxxx/ApiEstandar"
    elementFormDefault="qualified" attributeFormDefault="unqualified">
    
    <xs:complexType name="GenericRequest"/>
    
    <xs:complexType name="GenericResponse">
        <xs:sequence>
            <xs:element name="codigoRespuesta" type="xs:string" />
            <xs:element name="descripcionRespuesta" type="xs:string" />

尝试将XSD中xmlns:xs改为https://jakarta.ee/xml/ns/jaxb后,Eclipse提示无法解析xs:string,不确定如何正确配置。

未使用绑定文件,当前pom.xml中插件配置如下:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>xjc</id>
            <goals>
                <goal>xjc</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <locale>en</locale>
        <packageName>com.xxxxx.services.xxxxxservice.soap.services.model</packageName>
        <sources>
            <source>${project.basedir}/src/main/resources/xsd/APIEstandar_v1.0.xsd</source>
        </sources>
    </configuration>
</plugin>
解决方案

核心问题

jaxb2-maven-plugin 3.x版本对配置参数做了调整:

  • 旧版本中<sources>参数可用于指定XSD文件
  • 新版本中<sources>仅用于指定外部绑定文件(.xjb),XSD文件需通过<xsdSources>参数配置

你当前把XSD文件放在了<sources>中,插件会将其当作绑定文件解析,因此出现根元素不匹配的错误。

修复步骤

  1. 修改pom.xml中的插件配置,将<sources>替换为<xsdSources>:
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>xjc</id>
            <goals>
                <goal>xjc</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <locale>en</locale>
        <packageName>com.xxxxx.services.xxxxxservice.soap.services.model</packageName>
        <!-- 替换sources为xsdSources -->
        <xsdSources>
            <xsdSource>${project.basedir}/src/main/resources/xsd/APIEstandar_v1.0.xsd</xsdSource>
        </xsdSources>
    </configuration>
</plugin>
  1. 保留XSD文件的原始命名空间,不要修改xmlns:xs="http://www.w3.org/2001/XMLSchema",因为xs:string等类型属于W3C XML Schema标准命名空间,与Jakarta JAXB的命名空间无关。

完成以上修改后,重新执行mvn jaxb2:xjc即可正常生成DTO文件。

内容的提问来源于stack exchange,提问作者Paco Abato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 16:13:18