Java 在运行时为使用 wsimport 生成的代码覆盖或设置 Web 服务端点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3569075/
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
overriding or setting web service endpoint at runtime for code generated with wsimport
提问by user363808
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,步骤如下:
- I compile the java class and generate a war file
- Deploy the war file to my app server (tomcat)
- Access the WSDL via the URL e.g. localhost:8080/service/helloservice?wsdl
- use the URL with wsimport.bat to generate client classes for example:
wsimport http://localhost:8080/service/helloservice?Wsdl - I use those classes in my client app to call the service
- 我编译java类并生成一个war文件
- 将 war 文件部署到我的应用服务器 (tomcat)
- 通过 URL 访问 WSDL,例如 localhost:8080/service/helloservice?wsdl
- 使用带有 wsimport.bat 的 URL 来生成客户端类,例如:
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. 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.
问题是服务部署在运行在 8080 以外的端口上的应用服务器上,客户端和服务之间的通信永远不会发生。我想知道创建在客户端使用的存根中没有硬编码服务器和端口的存根的最佳方法是什么。
采纳答案by McDowell
Your client can set the end-point in the service "port" at runtime via the BindingProviderinterface.
您的客户端可以在运行时通过BindingProvider接口在服务“端口”中设置端点。
Consider the JAX-WS client in this JAX-WS tutorial. Another way to write this code would be:
考虑本 JAX-WS 教程中的 JAX-WS 客户端。编写此代码的另一种方法是:
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.
警告:我还没有下载教程代码并针对它测试了这段代码。
回答by user3749126
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 i.e. Windows C:........etc
我遇到了同样的问题,这很糟糕,因为一旦将代码移至生产环境,它总是寻找硬编码的 WSDL 位置,即 Windows C:........etc
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();

