java 如何创建具有动态端点的 CXF Web 服务客户端?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7447464/
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-30 20:00:48  来源:igfitidea点击:

How to create a CXF webservice client with dynamic endpoint?

javaweb-servicessoapcxfendpoint

提问by Marco

We have a central WSDL file that describes a web service. We use CXF to generate client code, but this code seems to be bound to 1 endpoint. How can i create a CXF client that uses the WSDL, but where I can specify the endpoint? Is there are way in changing the endpoint to a URL that implements the same WSD:?

我们有一个描述 Web 服务的中央 WSDL 文件。我们使用 CXF 生成客户端代码,但是这段代码似乎绑定到 1 个端点。我如何创建一个使用 WSDL 的 CXF 客户端,但我可以在哪里指定端点?有没有办法将端点更改为实现相同 WSD 的 URL:?

回答by Daniel Kulp

If the otherservice implements the same WSDL, when you create the MyClientServiceobject, you can pass the URL to the new service's WSDL right to the constructor and it will us it. Most services would expose its wsdl on ?wsdl so using that may "just work".

如果other服务实现了相同的 WSDL,那么当您创建MyClientService对象时,您可以将 URL 传递给新服务的 WSDL 权限给构造函数,它就会使用它。大多数服务会在 ?wsdl 上公开其 wsdl,因此使用它可能“正常工作”。

Alternatively, you can override the endpoint URL via:

或者,您可以通过以下方式覆盖端点 URL:

((BindingProvider)proxy).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_URL, "http://my.service.url.com/...")

where proxyis the MyClientServiceobject.

其中proxyMyClientService对象。

回答by Shahzad Mughal

Working in cxf 2.6.1

在 cxf 2.6.1 中工作

Client client = ClientProxy.getClient(port);
client.getRequestContext().put(Message.ENDPOINT_ADDRESS, "http://some-valid-endpoint") ;

回答by Atul

javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method XXXX.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:125)
at com.sun.proxy.$Proxy82.getUser(Unknown Source)

javax.xml.ws.WebServiceException: 找不到 Web 方法 XXXX 的 wsdl:binding 操作信息。
在 org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:125)
在 com.sun.proxy.$Proxy82.getUser(Unknown Source)

If this error occurred then add annotation Webservice to your client side interface which is already developed on service side.

如果发生此错误,请将注释 Webservice 添加到已在服务端开发的客户端界面中。

I faced this issue, I have configured Spring with CXF. I have already developed the service and calling it from client.

我遇到了这个问题,我已经用 CXF 配置了 Spring。我已经开发了服务并从客户端调用它。

Hope this will help.

希望这会有所帮助。

回答by Atul

you can use JaxWsProxyFactoryBeanfor dynamically calling a service

您可以使用JaxWsProxyFactoryBean动态调用服务

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

factory.setServiceClass(ChangeStudentDetails.class);
factory.setAddress("http://localhost:8081/CXFTutorial/ChangeStudent");