gpt4 book ai didi

wsdl 未提供 xsd 架构

转载 作者:行者123 更新时间:2023-12-04 12:11:20 25 4
gpt4 key购买 nike

我正在使用 JAX-WS 开发 WebService(我在 jaxws-maven-plugin 上使用 wsimport 目标)。我编写了一个导入 XSD 模式的 WSDL。

WEB-INF/wsdl/service.wsdl
WEB-INF/wsdl/service.xsd

我还生成了 Web 服务类并创建了端点等等。到目前为止一切都很好。当我在 Tomcat 7 上运行我的服务时,一切正常。我可以在浏览器中访问 wsdl:
http://localhost:8080/webService/servlet-url?wsdl

但我无法访问 xsd 架构。问题出在这个 wsdl 中:
<xsd:schema>
<xsd:import namespace="http://ws.service/domain/1.0" schemaLocation="service.xsd"/>
</xsd:schema>

当然,在生成类时 wsdl 和 xsd 位于本地路径上,但我希望在 Web 服务运行时可以远程访问它们。我知道 schemaLocation 应该是这样的“http://localhost:8080/webService/servlet-url?xsd=1”。

在浏览器导入中呈现的 wsdl 应该如下所示:
<xsd:schema>
<xsd:import namespace="http://ws.service/domain/1.0" schemaLocation="http://localhost:8080/webService/servlet-url?wsdl&resource=service.xsd"/>
</xsd:schema>

localhost:8080/webService/servlet?wsdl 给我:
wsdl:definitions targetNamespace="http://ws.serv.com/Service/1.0" name="emuiaService">         
<wsdl:types>
<xsd:schema>
<xsd:import namespace="http://ws.serv.com/Service/domain/1.0" schemaLocation="schema.xsd"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="halloMsg">
<wsdl:part name="parameters" element="dom:halloRequest"/>
</wsdl:message>
<wsdl:message name="halloResponseMsg">
<wsdl:part name="return" element="dom:halloResponse"/>
</wsdl:message>

等等...

最佳答案

我几乎不敢相信这是一个如此难以解决的问题!

我一直在疯狂地谷歌搜索以找到解决这个问题的方法!然后我一直在努力寻找自己的解决方案。通过调试器逐步执行 java-6-openjdk 的默认 javax.xml.ws.spi.Provider 实现(JRE 中的“工厂”,用于创建用于发布 Web 服务的 javax.xml.ws.Endpoint 对象)我终于学到了一些东西,这帮助我制定了一个至少适用于 Java SE 的解决方案,至少在我当前的 JRE 中是这样:

java version "1.6.0_33"
OpenJDK Runtime Environment (IcedTea6 1.13.5) (6b33-1.13.5-1ubuntu0.12.04)
OpenJDK Server VM (build 23.25-b01, mixed mode)

我还不知道这个解决方案是否可以在 Java EE 中使用。

这是我解决它的方法:
package myservice;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Endpoint;

public class App
{
private static final String MY_SERVICE_XSD = "/wsdl/MyService.xsd";

public static void main( String[] args )
{
Endpoint ep = Endpoint.create(new MyEndpointImpl());

ep.setMetadata(Arrays.asList(sourceFromResource(MY_SERVICE_XSD)));

ep.publish("http://localhost:8080/svc/hello");
}

private static Source sourceFromResource(String name) {
URL resource = App.class.getResource(name);
String systemId = resource.toExternalForm();
InputStream inputStream;
try {
inputStream = resource.openStream();
} catch (IOException e) {
throw new RuntimeException("Failed to create InputStream from resource \""+ name +"\"", e);
}
return new StreamSource(inputStream, systemId);
}
}

关键是我首先使用方法 Endpoint#create(而不是 Endpoint#publish)来获取未发布的端点。然后我将 XSD 文件作为“元数据”添加到(仍未发布的)端点(代码“ep.setMetaData(...)”)。然后我发布端点(代码“ep.publish(...)”)。

现在当我访问 http://localhost:8080/svc/hello?wsdl我得到:
    <definitions targetNamespace="http://somewhere.net/my/namespace" name="MyService">
<types>
<xsd:schema>
<xsd:import namespace="http://somewhere.net/my/namespace"
schemaLocation="http://localhost:8080/svc/hello?xsd=1"/>
</xsd:schema>
</types>
...
</definitions>

我的 XSD 文件可从 http://localhost:8080/svc/hello?xsd=1 获得!

请注意,磁盘上的 MyService.wsdl 文件仍包含:
            <xsd:schema>
<xsd:import namespace="http://somewhere.net/my/namespace"
schemaLocation="MyService.xsd"></xsd:import>
</xsd:schema>

关于wsdl 未提供 xsd 架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11013658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com