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

Java 在不下载WSDL的情况下实例化JAX-WS服务?,java,web-services,wsdl,jax-ws,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, se

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

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

    //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);

我通过在客户端中为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");
见:

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>
  [...]

org.jvnet.jax-ws-commons
jaxws-maven插件
2.3
我的服务
wsimport
-B-XautoNameResolution
德克斯
${basedir}\src\main\resources\META-INF\wsdl
MyService.wsdl
META-INF/wsdl/MyService.wsdl
[...]

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

我已经看到这也适用于服务的无参数构造函数-在您的例子中是WebService\u service()。我不知道什么是更好的实践,像你一样使用无参数构造函数或(null,QName)构造函数。
@WebServiceClient
注释有一个
wsdlLocation
属性,该属性包含指向WSDL的绝对路径。手动删除此项时(您确实不想这样做),我遇到了以下异常:
com.sun.xml.ws.streaming.XMLStreamReaderException:xml读取器错误:com.ctc.wstx.exc.WstxUnexpectedCharException:prolog中意外的字符“a”(代码97);顺便说一下,我的平台是WebLogic 12c。另外,你答案中的链接已经失效。谢谢@Dormouse。。。我会更新链接。我的域名必须更改:(我的解决方案类似,使用没有地址的虚拟wsdl,并在运行时设置有效地址: