JAX-WS在WSDL文件上出现错误:"解析组件's:schema'时发生错误"

24

错误

我在Java项目中使用wsimport为三个SOAP web服务生成源代码。前两个工作正常:我使用JAX-WS Maven插件来获取WSDL文件并生成相应的Java源代码。

其中一个Web服务失败了,我遇到了以下错误:

[jaxws:wsimport]
Processing: /home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl
jaxws:wsimport args: [-s, /home/me/NetBeansProjects/Admin/AdminWeb/target/generated-sources/jaxws-wsimport, -d, /home/me/NetBeansProjects/Admin/AdminWeb/target/classes, -verbose, -catalog, /home/me/NetBeansProjects/Admin/AdminWeb/src/jax-ws-catalog.xml, -wsdllocation, http://erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx?WSDL, -target, 2.0, -extension, -Xnocompile, /home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl]
parsing WSDL...


src-resolve.4.2: Error resolving component 's:schema'. It was detected that 's:schema' is in namespace 'http://www.w3.org/2001/XMLSchema', but components from this namespace are not referenceable from schema document 'file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl#types?schema1'. If this is the incorrect namespace, perhaps the prefix of 's:schema' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl#types?schema1'.
  line 80 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl#types?schema1

undefined element declaration 's:schema'
  line 80 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl

undefined element declaration 's:schema'
  line 127 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl

undefined element declaration 's:schema'
  line 142 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl

罪犯

这个 WSDL 文件和能够工作的文件之间的区别在于错误信息中标注的行号,即第 80、127 和 142 行:

<s:element ref="s:schema" />

注意:WSDL文件的根元素定义了“s”命名空间,如下所示:
xmlns:s="http://www.w3.org/2001/XMLSchema" 

我尝试过的方法

我进行了研究。看起来其他人也遇到了类似的问题,有些解决方案是“不要使用<s:element ref="s:schema" />”,有些是使用“import”标签,还有一些无法确定的解决方案,它们显然在旧的java.net论坛上(在现代亚历山大图书馆之火之前)。

  • I have tried putting the following import statement just inside the element that contains the problem tags: <s:import namespace="http://www.w3.org/2001/XMLSchema" schemaLocation="http://www.w3.org/2001/XMLSchema.xsd" />. wsimport gives me a new error:

    [jaxws:wsimport]
    Processing: /home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl
    jaxws:wsimport args: [-s, /home/me/NetBeansProjects/Admin/AdminWeb/target/generated-sources/jaxws-wsimport, -d, /home/me/NetBeansProjects/Admin/AdminWeb/target/classes, -verbose, -catalog, /home/me/NetBeansProjects/Admin/AdminWeb/src/jax-ws-catalog.xml, -wsdllocation, http://erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx?WSDL, -target, 2.0, -extension, -Xnocompile, /home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl]
    parsing WSDL...
    
    
    Element "{http://www.w3.org/2001/XMLSchema}annotation" shows up in more than one properties.
      line 248 of http://www.w3.org/2001/XMLSchema.xsd
    
    The following location is relevant to the above error
      line 242 of http://www.w3.org/2001/XMLSchema.xsd
    
    Property "Any" is already defined. Use &lt;jaxb:property> to resolve this conflict.
      line 108 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl
    
    The following location is relevant to the above error
      line 109 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl
    
    Property "Any" is already defined. Use &lt;jaxb:property> to resolve this conflict.
      line 184 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl
    
    The following location is relevant to the above error
      line 185 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl
    
    Property "Any" is already defined. Use &lt;jaxb:property> to resolve this conflict.
      line 199 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl
    
    The following location is relevant to the above error
      line 200 of file:/home/me/NetBeansProjects/Admin/AdminWeb/src/wsdl/erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl
    

    Lines 108 and 109 referenced in this error are: (lines 184-5, 199-200 are similar)

    <s:any minOccurs="0" maxOccurs="unbounded" namespace="http://www.w3.org/2001/XMLSchema" processContents="lax" />
    <s:any minOccurs="1" namespace="urn:schemas-microsoft-com:xml-diffgram-v1" processContents="lax" />
    
  • I have tried upgrading jaxws-maven-plugin from 1.10 to 2.2. Same problem.

  • Here's a possible solution -- I'm trying to figure out how to implement this using the JAX-WS Maven plugin. Any hints?

结论

有什么想法吗?还需要更多的信息吗?为了简洁起见,我省略了pom.xml和Service.asmx.wsdl文件,但如果它们包含更重要的信息,我可以将它们包括在内。

谢谢!


附录

这里有另一个遇到同样问题的人,如果这对任何潜在的回答者有帮助。 这是另一个类似的问题。 我不太理解这篇文章,但它似乎意味着我必须手动解析SOAP XML!太可怕了!


这可能是由于JAXP-14错误 ... 有没有已知的解决方法? - Nick
5个回答

16

我通过将Vivek Pandey的方法 应用于Maven,并升级至jaxws-maven-plugin 2.2来解决这个问题。以下是针对后人的重申:

摘要

此XJB自定义文件(见下文)放入您的默认绑定文件目录中,并设置wsimport绑定它和http://www.w3.org/2001/XMLSchema.xsd

XJB自定义

上述XML文件xsd.xjb的内容应放在您的默认绑定文件目录中,如下所示(感谢Kohsuke):

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
          version="2.0">

  <globalBindings>
    <xjc:simple />
  </globalBindings>

  <bindings scd="~xsd:complexType">
    <class name="ComplexTypeType"/>
  </bindings>

  <bindings scd="~xsd:simpleType">
    <class name="SimpleTypeType"/>
  </bindings>

  <bindings scd="~xsd:group">
    <class name="GroupType"/>
  </bindings>

  <bindings scd="~xsd:attributeGroup">
    <class name="AttributeGroupType"/>
  </bindings>

  <bindings scd="~xsd:element">
    <class name="ElementType"/>
  </bindings>

  <bindings scd="~xsd:attribute">
    <class name="attributeType"/>
  </bindings>
</bindings>

POM

这是我的 POM 文件的相关部分,已标明更改:

<plugin>
    <!-- CHANGE: updated groupId and version -->
    <groupId>org.jvnet.jax-ws-commons</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution> 
            <goals>
                <goal>wsimport</goal>
            </goals>
            <configuration>
                <!-- CHANGE: added args tag to bind http://www.w3.org/2001/XMLSchema.xsd -->
                <args>
                    <arg>-b</arg><arg>http://www.w3.org/2001/XMLSchema.xsd</arg>
                </args>
                <wsdlFiles>
                    <wsdlFile>erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl</wsdlFile>
                </wsdlFiles>
                <wsdlLocation>http://erp-app-devel.srv.mycompany.ca/EgTestReportEngine/Service.asmx.wsdl</wsdlLocation>
                <staleFile>${project.build.directory}/jaxws/stale/Service.asmx.stale</staleFile>
                <!-- CHANGE: added bindingFiles tag to bind XJB customization, located at the default binding files directory, MyProject/src/jaxws/xsd.xjb . -->
                <bindingFiles>
                    <bindingFile>xsd.xjb</bindingFile>
                </bindingFiles>
            </configuration>
            <id>wsimport-generate-egtestreportengine</id>
            <phase>generate-sources</phase>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>webservices-api</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
    <configuration>
        <sourceDestDir>${project.build.directory}/generated-sources/jaxws-wsimport</sourceDestDir>
        <xnocompile>true</xnocompile>
        <verbose>true</verbose>
        <extension>true</extension>
        <catalog>${basedir}/src/jax-ws-catalog.xml</catalog>
        <target>2.0</target>
    </configuration>
</plugin>

1
除非我弄错了,pom文件中的bindingFiles部分和wsimport命令行参数中的-b执行相同的操作。为什么不将它们合并呢?提供的链接中的wsimport命令(实际上)是wsimport -b http://www.w3.org/2001/XMLSchema.xsd -b xsd.xjb <TARGET-WSDL> - jpaugh
另外,感谢您发布您的答案!它非常有效!特别感谢您挽救了 XJB 免于位腐。 - jpaugh
1
@jpaugh 非常欢迎!我非常高兴,在三年半之后,我对某人有所帮助:D - Nick

9
为了让Nick的解决方案起作用,您可能需要将这两个jvm参数添加到您的pom.xml文件中。因此,在遇到这些错误时:
org.xml.sax.SAXParseException; systemId: http://www.w3.org/2001/XMLSchema.xsd; lineNumber: 67; columnNumber: 11; External DTD: Failed to read external DTD 'XMLSchema.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property. [WARNING] schema_reference: Failed to read schema document 'xml.xsd', because 'http' access is not allowed due to restriction set by the accessExternalSchema property. line 91 of http://www.w3.org/2001/XMLSchema.xsd 只需将它们添加到您的pom.xml文件中即可。
-Djavax.xml.accessExternalDTD=all
-Djavax.xml.accessExternalSchema=all

<vmArgs>
    <vmArg>-Djavax.xml.accessExternalDTD=all</vmArg>
    <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
</vmArgs>

但是,依赖外部资源对于生产系统来说大多数情况下都不是一个好主意。如果需要重新构建并且无法访问外部资源怎么办? - Robin Jonsson
只需将<vmargs>块添加到pom.xml即可。 - Milan Oparnica

2

我遇到了相同的问题,用下面的命令解决了:

wsimport -b http://www.w3.org/2001/XMLSchema.xsd -b xsd.xjb service.wsdl

其中xsd.xjb指的是:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
          version="2.0">

  <globalBindings>
    <xjc:simple />
  </globalBindings>

  <bindings scd="~xsd:complexType">
    <class name="ComplexTypeType"/>
  </bindings>

  <bindings scd="~xsd:simpleType">
    <class name="SimpleTypeType"/>
  </bindings>

  <bindings scd="~xsd:group">
    <class name="GroupType"/>
  </bindings>

  <bindings scd="~xsd:attributeGroup">
    <class name="AttributeGroupType"/>
  </bindings>

  <bindings scd="~xsd:element">
    <class name="ElementType"/>
  </bindings>

  <bindings scd="~xsd:attribute">
    <class name="attributeType"/>
  </bindings>
</bindings>

1

如果您实际上不关心模型数据的这个特定部分,您可能可以使用JAXB绑定文件告诉JAXB将有问题的部分映射到类型为DOM Element的属性,而不是尝试将它们数据绑定到普通的JAXB类中。非官方JAXB指南中有一节介绍了这种技术。


谢谢。我在答案中详细说明的方法实际上可能归结为这个;我不太了解JAXB,无法确定。 - Nick

0

这对我来说似乎终于起作用了。我正在使用Apache Maven CFX插件。

    <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <additionalJvmArgs>
                                -Djavax.xml.accessExternalDTD=all
                            </additionalJvmArgs>
                            <sourceRoot>${basedir}/src/main/generated</sourceRoot> 
                            <wsdlOptions>
                                <wsdlOption>
                                    <extraargs>
                                        <extraarg>-autoNameResolution</extraarg>
                                        <extraarg>-impl</extraarg>
                                        <extraarg>-verbose</extraarg>
                                        <extraarg>-b</extraarg>
                                        <extraarg>http://www.w3.org/2001/XMLSchema.xsd</extraarg>

                                        <extraarg>-p</extraarg>
                                        <extraarg>com.nevado.travelstudio</extraarg>
                                    </extraargs>
                                    <!-- <bindingFiles> <bindingFile>${basedir}/src/main/resources/wsdl/mybindings.xjb</bindingFile> 
                                        </bindingFiles> -->
                                    <wsdl>${basedir}/src/main/resources/wsdl/B2.wsdl</wsdl>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

随着这些变化,我不得不在..java/jdk/jre/lib文件夹中创建jaxp.properties文件,并设置以下属性

  -Djavax.xml.accessExternalDTD=all

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接