[英]overriding or setting web service endpoint at runtime for code generated with wsimport
Using code that was generated with wsimport
, can the service endpoint be overridden without having to regenerate the code? 使用
wsimport
生成的代码,是否可以重写服务端点而无需重新生成代码?
I have written a simple java webservice, following are the steps: 我写了一个简单的java webservice,以下是步骤:
wsimport http://localhost:8080/service/helloservice?Wsdl
wsimport http://localhost:8080/service/helloservice?Wsdl
The problem is that is the service is deployed on an app server running on port other than 8080, the communication between client and service never happens. 问题是服务是部署在8080以外的端口上运行的应用服务器上,客户端和服务之间的通信永远不会发生。 I am trying to know what is the best way to create stubs that does not have server and port hardcoded in the stub used by the client.
我想知道在客户端使用的存根中创建没有服务器和端口硬编码的存根的最佳方法是什么。
Your client can set the end-point in the service "port" at runtime via the BindingProvider interface. 您的客户端可以在运行时通过BindingProvider接口在服务“端口”中设置端点 。
Consider the JAX-WS client in this JAX-WS tutorial . 在这个JAX-WS教程中考虑JAX-WS客户端。 Another way to write this code would be:
编写此代码的另一种方法是:
HelloService service = new HelloService();
Hello port = service.getHelloPort();
BindingProvider bindingProvider = (BindingProvider) port;
bindingProvider.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://foo:8086/HelloWhatever");
String response = port.sayHello(name);
Caveat: I haven't downloaded the tutorial code and tested this code against it. 警告:我没有下载教程代码并针对它测试了这段代码。
I faced the same issue, and it was terrible coz once the code is moved to production it always looked for the hardcoded WSDL location ie Windows C:........etc 我遇到了同样的问题,而且一旦代码转移到生产环节它很糟糕,它总是寻找硬编码的WSDL位置,即Windows C:........等
I have gone thru various post and pages to find the answer however all was failing then found myself a way by looking at the Service Class generated by JAX-WS imports. 我已经通过各种帖子和页面找到答案但是所有都失败了然后通过查看由JAX-WS导入生成的服务类找到了自己的方式。
I had to override the JAX-WS WSDL location implementation in my calling class like this. 我不得不像这样覆盖我的调用类中的JAX-WS WSDL位置实现。
URL baseUrl;
URL wsdlURL = null;
baseUrl = <your Services>.class.getResource(".");
try {
wsdlURL = new URL(baseUrl, "http://<your path>?wsdl");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
<your Services> yourServices = new <your Services(wsdlURL,new QName("your namespace", "<your service name>"));
System.out.println(Services.getWSDLDocumentLocation());
YourInterface YourInterfacePort = yourServices.getServicePort();
BindingProvider bindingProvider = (BindingProvider)YourInterfacePort;
bindingProvider.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
YourInterfacePort.methods(); YourInterfacePort.methods();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.