实例化 JAX-WS 服务而不下载 WSDL?

2022-09-03 06:09:09

我有一个Web服务,我有JAX-WS生成的客户机绑定,如下所示:

// web service client generated by JAX-WS
@WebServiceClient( ... )
public class WebService_Service extends Service {

    public WebService_Service(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    WebService getWebServiceSOAP() {
        // ...
    }
}

我希望能够创建一个指向远程服务的实例,例如:

WebService_Service svc = new WebService_Service(
    new URL("http://www.example.com/ws?wsdl"),
    new QName("http://www.example.com/ws", "WebService"));

但是,这下载了我不想做的WSDL。http://www.example.com/ws?wsdl

有没有办法停止下载该 WSDL,但仍然指向同一端点?


答案 1

我通过在客户端中为 WSDL URL 指定 null 以及显式指定端点地址来解决此问题:

WebService_Service svc = new WebService_Service(
  null,
  new QName("http://www.example.com/ws", "WebService"));
WebService port = svc.getPort(WebService.class);
BindingProvider bindingProvider = (BindingProvider) port;
bindingProvider.getRequestContext()
  .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
    "http://www.example.com/real_endpoint_url_goes_here");

请参见: http://shrubbery.homeip.net/c/display/W/Consuming+a+Web+Service+with+Java+6+and+JAX-WS#ConsumingaWebServicewithJava6andJAX-WS-IgnoringtheWSDLCompletely


答案 2

WSDL 文件可能包含生成的存根不包含的配置选项,因此在运行时需要它们。您可以在类路径中本地提供它们。

以下maven pom.xml将我正在使用的服务的WSDL文件放入我的文件夹后为我工作:${basedir}\src\main\resources\META-INF\wsdl

<plugin>
  <groupId>org.jvnet.jax-ws-commons</groupId>
  <artifactId>jaxws-maven-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
      <id>MyService</id>
      <goals>
        <goal>wsimport</goal>
      </goals>
      <configuration>
        <!-- this resolves naming conflicts within the wsdl - there are several copies of fault report objects which clash otherwise. -->
        <args>
          <arg>-B-XautoNameResolution</arg>
        </args>

        <packageName>de.xyz</packageName>
        <wsdlDirectory>${basedir}\src\main\resources\META-INF\wsdl</wsdlDirectory>
        <wsdlFiles>
          <wsdlFile>MyService.wsdl</wsdlFile>
        </wsdlFiles>
        <wsdlLocation>META-INF/wsdl/MyService.wsdl</wsdlLocation>                           
      </configuration>
    </execution>
  [...]

在运行时,将从类路径加载 wsdl 文件。


推荐