Java 如何更改 webservice url 端点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2490737/
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
How to change webservice url endpoint?
提问by EugeneP
I generated a web-service client using JBoss utils (JAX-WS compatible) using Eclipse 'web service client from a wsdl'.
我使用 Eclipse 'web service client from a wsdl' 使用 JBoss utils(JAX-WS 兼容)生成了一个 web 服务客户端。
So, the only thing I provided was a url to a web-service WSDL.
因此,我提供的唯一内容是 Web 服务 WSDL 的 url。
Now, the web service provider tells me to change the "url of client endpoint application access" of the web-service.
现在,Web 服务提供商告诉我更改 Web 服务的“客户端端点应用程序访问的 url”。
What is it and how to change it?
它是什么以及如何改变它?
采纳答案by Pascal Thivent
IMO, the provider is telling you to change the service endpoint (i.e. where to reach the web service), not the client endpoint (I don't understand what this could be). To change the service endpoint, you basically have two options.
IMO,提供商告诉您更改服务端点(即到达 Web 服务的位置),而不是客户端端点(我不明白这可能是什么)。要更改服务端点,您基本上有两种选择。
Use the Binding Provider to set the endpoint URL
使用 Binding Provider 设置端点 URL
The first option is to change the BindingProvider.ENDPOINT_ADDRESS_PROPERTY
property value of the BindingProvider
(every proxy implements javax.xml.ws.BindingProvider
interface):
第一个选项是更改(每个代理实现接口)的BindingProvider.ENDPOINT_ADDRESS_PROPERTY
属性值:BindingProvider
javax.xml.ws.BindingProvider
...
EchoService service = new EchoService();
Echo port = service.getEchoPort();
/* Set NEW Endpoint Location */
String endpointURL = "http://NEW_ENDPOINT_URL";
BindingProvider bp = (BindingProvider)port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
System.out.println("Server said: " + echo.echo(args[0]));
...
The drawback is that this only works when the original WSDL is still accessible. Not recommended.
缺点是这仅在原始 WSDL 仍可访问时才有效。不建议。
Use the WSDL to get the endpoint URL
使用 WSDL 获取端点 URL
The second option is to get the endpoint URL from the WSDL.
第二个选项是从 WSDL 获取端点 URL。
...
URL newEndpoint = new URL("NEW_ENDPOINT_URL");
QName qname = new QName("http://ws.mycompany.tld","EchoService");
EchoService service = new EchoService(newEndpoint, qname);
Echo port = service.getEchoPort();
System.out.println("Server said: " + echo.echo(args[0]));
...
回答by Femi
To change the end address property edit your wsdl file
要更改结束地址属性,请编辑您的 wsdl 文件
<wsdl:definitions.......
<wsdl:service name="serviceMethodName">
<wsdl:port binding="tns:serviceMethodNameSoapBinding" name="serviceMethodName">
<soap:address location="http://service_end_point_adress"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
回答by MattC
To add some clarification here, when you create your service, the service class uses the default 'wsdlLocation', which was inserted into it when the class was built from the wsdl. So if you have a service class called SomeService, and you create an instance like this:
在这里添加一些说明,当您创建您的服务时,服务类使用默认的“wsdlLocation”,它是在从 wsdl 构建类时插入的。因此,如果您有一个名为 SomeService 的服务类,并且您创建了一个这样的实例:
SomeService someService = new SomeService();
If you look inside SomeService, you will see that the constructor looks like this:
如果您查看 SomeService 内部,您将看到构造函数如下所示:
public SomeService() {
super(__getWsdlLocation(), SOMESERVICE_QNAME);
}
So if you want it to point to another URL, you just use the constructor that takes a URL argument (there are 6 constructors for setting qname and features as well). For example, if you have set up a local TCP/IP monitor that is listening on port 9999, and you want to redirect to that URL:
因此,如果您希望它指向另一个 URL,您只需使用带有 URL 参数的构造函数(还有 6 个构造函数用于设置 qname 和 features)。例如,如果您设置了一个侦听端口 9999 的本地 TCP/IP 监视器,并且您想重定向到该 URL:
URL newWsdlLocation = new URL("http://theServerName:9999/somePath");
SomeService someService = new SomeService(newWsdlLocation);
and that will call this constructor inside the service:
这将在服务内部调用这个构造函数:
public SomeService(URL wsdlLocation) {
super(wsdlLocation, SOMESERVICE_QNAME);
}
回答by TastyWheat
I wouldn't go so far as @Femi to changethe existing address property. You can add new services to the definitions section easily.
我不会像@Femi 那样更改现有的地址属性。您可以轻松地将新服务添加到定义部分。
<wsdl:service name="serviceMethodName_2">
<wsdl:port binding="tns:serviceMethodNameSoapBinding" name="serviceMethodName">
<soap:address location="http://new_end_point_adress"/>
</wsdl:port>
</wsdl:service>
This doesn't require a recompile of the WSDL to Java and making updates isn't any more difficult than if you used the BindingProvider option (which didn't work for me btw).
这不需要将 WSDL 重新编译为 Java,并且进行更新并不比使用 BindingProvider 选项(顺便说一句,这对我不起作用)更困难。