java 在运行时更改 WebService 端点地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2046790/
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
Change WebService endpoint address at run time
提问by Chan Pye
I used Netbeans to generate Web Sevice client code from WSDL url. But I can't change endpoint address at run time using code.
我使用 Netbeans 从 WSDL url 生成 Web 服务客户端代码。但是我无法在运行时使用代码更改端点地址。
Please help me to solve that problem!
请帮我解决这个问题!
回答by Chandra Patni
You can do it two ways:
你可以通过两种方式做到这一点:
1) Cast portto BindingProviderand specify BindingProvider.ENDPOINT_ADDRESS_PROPERTYproperty
1) 投射port到BindingProvider并指定BindingProvider.ENDPOINT_ADDRESS_PROPERTY属性
MyService service = new MyService();
MyPort port = service....
BindingProvider bindingProvider = (BindingProvider) port;
bindingProvider.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://New.Endpoint/service");
2) You can call the generated service constructor which takes WSDL URL as parameter.
2) 您可以调用生成的服务构造函数,该构造函数以 WSDL URL 作为参数。
QName qname = new QName("http://serviceuri/", "service");
String wsdl = "http://New.Endpoint/service?wsdl";
MyServiec service = new MyServiec(new URL(wsdl), qname);
MyPort port = check...;
回答by peterh
You can do it all from you service's constructor. JAX-WS will have generated various forms of the constructor for you. This is basically same as what is mentioned as solution #2 in Chandra's answer above.
您可以从服务的构造函数中完成所有操作。JAX-WS 将为您生成各种形式的构造函数。这与上面 Chandra 的回答中提到的解决方案 #2 基本相同。
However I do not think you want code the namespace and the service name once again as is done in that answer. Simply do:
但是,我认为您不想像该答案中那样再次对命名空间和服务名称进行编码。简单地做:
URL wsdl = new URL("http://New.Endpoint/service?wsdl");
MyService wsService = new MyService(wsdlURL);

