在不下载WSDL的情况下实例化JAX-WS服务?

时间:2011-11-10 20:27:10

标签: java web-services wsdl jax-ws

我有一个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"));

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

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

3 个答案:

答案 0 :(得分:19)

我通过在客户端中为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

答案 1 :(得分:1)

我遇到了同样的问题而且我解决了这个问题,但是我无法用你的样本揭示它,因为它取决于wsdl。

这是我的代码,跟踪解决方案:

    //This is the input object for the webservice
    GetDocumentInfoInput input = new GetDocumentInfoInput();
    input.setBarcode(barcode);
    //I instantiate the WS
    MAKSpcIntSpcWFSpcScannerInfo_Service service  = new MAKSpcIntSpcWFSpcScannerInfo_Service();
    //I get the WS port
    MAKSpcIntSpcWFSpcScannerInfo         port     = service.getMAKSpcIntSpcWFSpcScannerInfo();
    WSBindingProvider                    provider = (WSBindingProvider)port;
    //This is the row what set the URL for the WS
    provider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
    //This is the WS calling
    GetDocumentInfoOutput                output   = port.getDocumentInfo(input);

答案 2 :(得分:1)

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

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

<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文件。