java 使用已知但无法访问的 wsdl 创建 Web 服务客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2097789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:25:48  来源:igfitidea点击:

Creating a web-service client with a known but inaccessible wsdl

javaweb-servicesjbosswsdlclient

提问by jgrowl

We have been provided with a wsdl and xsd schema by a company we are working with via email. The web-services we are interfacing with are accessed through a IPsec tunnel. There are local references(on their end) in the published WSDL which means we cannot consume it.

与我们合作的一家公司通过电子邮件向我们提供了 wsdl 和 xsd 架构。我们所连接的网络服务是通过 IPsec 隧道访问的。已发布的 WSDL 中有本地引用(在它们的一端),这意味着我们无法使用它。

1st question: Is this a common setup? I thought the point of having a WSDL was not only to define the contract but to also expose the service to consumers.

第一个问题:这是一个常见的设置吗?我认为拥有 WSDL 的意义不仅在于定义契约,还在于向消费者公开服务。

I can easily generate client/server code off of the provided WSDL using wsimport, wsconsume, etc.. I know when my generated client makes a call to my generated service it produces the correct message I need.

我可以使用 wsimport、wsconsume 等轻松地从提供的 WSDL 生成客户端/服务器代码。我知道当我生成的客户端调用我生成的服务时,它会生成我需要的正确消息。

2nd Question: Is there an easy way to route this to a different soap address?

第二个问题:有没有一种简单的方法可以将其路由到不同的soap地址?

I just want to be able to do something like:

我只想能够做这样的事情:

SalesTaxService svc = new SalesTaxService();
SalesTax tax = svc.getSalesTaxPort()
tax.getRate("NY");

But not use the soap address defined in the WSDL. I would like to avoid writing a bunch of dispatch clients for each method.

但是不要使用WSDL 中定义的soap 地址。我想避免为每种方法编写一堆调度客户端。

Am I missing something?

我错过了什么吗?

*In response to skaffman: This is what was generated. It defaulted to wsdlLocation as a name shrug

*回应 skaffman:这就是生成的内容。它默认为 wsdlLocation 作为名称耸肩

   @WebServiceClient(name = "SomeService")
   public class SomeService_Service extends Service {

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

    public SomeService_Service(URL wsdlLocation) {
        super(wsdlLocation, new QName("urn:some_service", "SomeService"));   
    }
  }

采纳答案by jgrowl

So I figured out why I was having a problem. I was assuming that the wsdlLocation had to be the WSDL that the actual service was publishing. This of course is not the case. The solution is to package a local WSDL with the correct SOAP:Address for the actual service into the client.

所以我想出了为什么我有问题。我假设 wsdlLocation 必须是实际服务发布的 WSDL。这当然不是这种情况。解决方案是将本地 WSDL 与实际服务的正确 SOAP:Address 打包到客户端中。

editI found out that you can alter the endpoint address programmatically without having to alter the actual WSDL:

编辑我发现您可以以编程方式更改端点地址而无需更改实际的 WSDL:

HelloService service = new HelloService (
  this.getClass().getResource("originalHello.wsdl"),
  new QName("http://example.org/hello", "HelloService "));
HelloPort proxy = service.getHelloPort();

Map<String, Object> ctxt = ((BindingProvider)proxy ).getRequestContext();
ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192);
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://new/endpointaddress");

proxy.sayHello("Hello World!");

Credit goes to : Jianming Li

致谢:李建明

回答by skaffman

I thought the point of having a WSDL was not only to define the contract but to also expose the service to consumers.

我认为拥有 WSDL 的意义不仅在于定义契约,还在于向消费者公开服务。

No, WSDL is purely a descriptive tool, it has no real runtime role. The web service operates completely independently of the WSDL. It's not uncommon for the WSDL to not be exposed.

不,WSDL 纯粹是一个描述性工具,它没有真正的运行时角色。Web 服务完全独立于 WSDL 运行。不公开 WSDL 的情况并不少见。

Is there an easy way to route this to a different soap address?

有没有一种简单的方法可以将其路由到不同的soap地址?

That depends entirely on what web service implementation you're using, and you don't say, although I'm guessing JAX-WS. If that's the case, the artifacts that JAX-WS's tools generate allow you to pass in the URL to the client stub constructors, I think.

这完全取决于您使用的 Web 服务实现,您不会说,尽管我猜是 JAX-WS。如果是这种情况,我认为 JAX-WS 的工具生成的工件允许您将 URL 传递给客户端存根构造函数。