【问题标题】:How to invoke web services on WSDL URL in Java?如何在 Java 中调用 WSDL URL 上的 Web 服务?
【发布时间】:2013-03-03 15:47:53
【问题描述】:

我需要在我正在构建的 Java Web 应用程序中调用一些 Web 服务方法。

例如,每次用户注册时,我想通过 Java 在 WSDL url 上调用 newUser 方法。我需要在请求中传递一些参数。

是否有任何内置的 Java 类或任何公开可用的类可以使这变得简单,即我只提供 URL 和参数,然后它执行请求并返回响应?

如果不是,在 Java 应用程序中调用 WSDL 上的 Web 服务的标准方法是什么?

【问题讨论】:

  • 您可以使用 Apache Commons HTTP 客户端发出 POST 请求。 SOAP 服务使用 HTTP。

标签: java web-services soap wsdl playframework-2.0


【解决方案1】:

在部署的 WSDL URL 上运行 wsimport,你可以从你的 JDK 运行它:

wsimport -p client -keep http://localhost:8080/calculator?wsdl

这一步将生成和编译一些类。注意 -keep 开关,你需要它来保存生成的 Java 源文件。

Calculator.java - 服务端点接口或 SEI
CalculatorService - 生成的服务,实例化它

public class MyClientServiceImpl {
    public static void main(String args[]){

    @Override
    public Integer add(int a , int b) {
       CalculatorService service = new CalculatorService();
       Calculator calculatorProxy = service.getCalculatorPort();            
        /**
         * Invoke the remote method
         */
        int result = calculatorProxy.add(10, 20);
        System.out.println("Sum of 10+20 = "+result);
    }
}

如果您使用的是 Java EE 6 支持的容器,那么您可以通过这种方式使用它,

public class MyClientServiceImpl implements MyClientService {

    @WebServiceRef(wsdlLocation = "http://localhost:8080/calculator?wsdl", 
value = CalculatorService.class)
    private Calculator service;

    @Override
    public Integer add(int a , int b) {
        return service.add(a,b);
    }
}

【讨论】:

  • 如何安装/运行 WSImport?我在 Linux 上。
  • 在你的 linux 上运行 $ which java,你需要从 linux 的 JDK 目录启动它,docs.oracle.com/javase/6/docs/technotes/tools/share/…
  • 谢谢,它现在可以工作了。您能否删除包含 URL 的 cmets?然后我会接受。
  • 我已经删除了带有 WSDL url 的 cmets
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-19
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-01
相关资源
最近更新 更多