未定义元素声明'xs:schema'

8

我完全不了解网络服务相关的知识。

我需要为一个网络服务编写 rest web 服务客户端。该网络服务在 SoapUI 上运行良好。我已经获得了该 URL 的 WSDL 文件。但是,当我将 wsdl 文件添加到我的 Eclipse 项目中时,它却出现了编译错误。

src-resolve.4.2: Error resolving component 'xs:schema'. It was detected that 'xs:schema' is in namespace 'http://www.w3.org/2001/XMLSchema', but components from this namespace are not referenceable from schema document 'file:///E:/Ashish%20Workspace/HATStoLSAMS%20Webservice/HATS2LSAMSWS/WebContent/WEB-INF/wsdl/CorpsiteService.svc.wsdl'. If this is the incorrect namespace, perhaps the prefix of 'xs:schema' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///E:/Ashish%20Workspace/HATStoLSAMS%20Webservice/HATS2LSAMSWS/WebContent/WEB-INF/wsdl/CorpsiteService.svc.wsdl'.

我谷歌了很多次想要解决这些错误,但是没有任何作用。如果我无视这些错误,并且尝试使用wsimport和wsdl2java命令创建存根,它会报错。

[ERROR] undefined element declaration 'xs:schema'
line 1 of http://chec.local/STAR.WCF/CorpsiteService.svc?singlewsdl

我正在使用以下命令生成存根:
wsimport -d e:\test -s E:\wssrc http://chec.local/STAR.WCF/CorpsiteService.svc?singlewsdl -wsdllocation "../../../../../WEB-INF/wsdl/CorpsiteService.svc.wsdl"

我卡在这一点上,已经挣扎了整整一天。 对于此事的任何帮助都将非常有用。

4个回答

10

解决方法似乎是为xs:schema提供替代绑定,如https://metro.java.net/2.1/guide/Dealing_with_schemas_that_are_not_referenced.html中所述。

具体而言,对于通常导入到命名空间xs中的http://www.w3.org/2001/XMLSchema,需要进行一些额外的工作。

命令应为:wsimport -b http://www.w3.org/2001/XMLSchema.xsd -b customization.xjb something.wsdl

上面链接的customization.xjb位于https://www.java.net//blog/kohsuke/archive/20070228/xsd.xjb或从下面复制。

这个定制存在的目的是为了处理在使用模式 Schema 时可能出现的一些命名冲突(该模式作为多个模式中的相同名称空间导入)。

customization.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>

我已经尝试使用这些文件和相关的-b参数来运行wsimport,并且似乎已经解决了这个错误(并进入了其他错误)。 话虽如此,我并不100%确定我的新错误是否部分原因是由于此问题引起的(因此这不是一个完整的答案)。 没有实际导致问题的wsdl,人们只能合理猜测解决问题(并进入下一个问题)。


1
感谢@MichaelT提供简明的答案和链接,这解决了我的问题。 - jlr
1
@jlr,感谢您让我知道它很有用。有了这个xsd,我从未能解决错误,并转而使用Apache Axis库来执行更具容错性/动态的客户端。话虽如此,它在客户端方面也更加灵活,没有wsimport提供的易于使用的静态类型。 - user289086
我没有使用wsimport,而是使用Apache CXF的wsdl2java工具来生成这些代码。我避免使用Glassfish,因为我过去曾遇到一些与其无关的问题(关于自定义数据序列化,无论我尝试什么都无法解决,而CXF则可以直接使用!)。 - jlr

4
如果您使用的是maven-jaxb2-plugin而不是“-b”,则需要在schemas中提供所需URL作为附加的模式:
<schema>
  <url>https://example.com/WebService.asmx?WSDL</url>
</schema>
<schema>
  <url>http://www.w3.org/2001/XMLSchema.xsd</url>
</schema>

您甚至可以将其保存在资源中,这样它就会自动被捕获。


1
这对我来说解决了这个问题。 - Erik Paula

2

我曾经遇到过同样的问题,只需在maven插件中添加一行代码即可解决。

原始答案:Original Answer

<args> 
    <arg>-b</arg>
    <arg>http://www.w3.org/2001/XMLSchema.xsd</arg>
</args>

下面是我的Maven插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <id>periodictableaccessws</id>
            <goals>
                <goal>wsimport</goal>
            </goals>
            <configuration>
                <wsdlDirectory>${basedir}/src/main/resources/wsdl</wsdlDirectory>
                <args>
                    <arg>-b</arg>
                    <arg>http://www.w3.org/2001/XMLSchema.xsd</arg>
                </args>
                <wsdlFiles>
                    <wsdlFile>doosdaas.wsdl</wsdlFile>
                </wsdlFiles>
                <packageName>com.dss.doosdaas</packageName>
                <vmArgs>
                    <vmArg>-Djavax.xml.accessExternalDTD=all</vmArg>
                    <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
                </vmArgs>
                <!--   <bindingDirectory>${basedir}/src/main/resources/jaxb</bindingDirectory>
                   <bindingFiles>
                       <bindingFile>jaxb_binding.xjb</bindingFile>
                   </bindingFiles>-->
            </configuration>
        </execution>
    </executions>
</plugin>

0

在我必須使用某些UE稅務海關網絡服務時,我遇到了這種錯誤。在之前的項目中,我使用了user289086提出的解決方案。

但對於最近的項目,我使用了另一種基於繫結的更簡短的解決方案,在這裡我使用了描述在使用JAXB數據綁定中的包裝器風格規則。

1- 創建一個帶有以下內容的綁定文件並將其添加到META-INF文件夾中:

<jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
    <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
</jaxws:bindings>

2- 使用wsimport从wsdl创建Web服务引用(在我的情况下,我使用Netbeans IDE WS Client Assistant,在后台使用wsimport)。例如,对于以下服务: UE CheckTinService

注意:首先,wsimport的过程可能会出现错误或不出现错误,但添加或不添加前面的绑定的差异很容易检查。

3- 将绑定引用添加到.pom文件中相应的jaxws-maven-plugin执行中。例如,它保持不变:

<plugin>
    <groupId>org.jvnet.jax-ws-commons</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <goals>
                <goal>wsimport</goal>
            </goals>
            <configuration>
                <wsdlFiles>
                    <wsdlFile>ec.europa.eu/taxation_customs/tin/checkTinService.wsdl</wsdlFile>
                </wsdlFiles>
                <packageName></packageName>
                <vmArgs>
                    <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
                </vmArgs>
                <wsdlLocation>https://ec.europa.eu/taxation_customs/tin/checkTinService.wsdl</wsdlLocation>
                <staleFile>${project.build.directory}/jaxws/stale/checkTinService.stale</staleFile>
                <bindingFiles>
                    <bindingFile>${basedir}/src/main/resources/META-INF/TaxationCustomsWsImportBindings.xml</bindingFile>
                </bindingFiles>
            </configuration>
            <id>wsimport-generate-checkTinService</id>
            <phase>generate-sources</phase>
        </execution>
    </executions>
    ...

您可以查看使用之前创建的绑定文件的确切配置:

<bindingFiles>
       <bindingFile>${basedir}/src/main/resources/META-INF/TaxationCustomsWsImportBindings.xml</bindingFile>
</bindingFiles>

4- 最后刷新WS引用,更改将应用于wsimport的过程。


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