Java调用CXF WebService接口的两种方式实例

44次阅读
没有评论

共计 778 个字符,预计需要花费 2 分钟才能阅读完成。

方式一:使用 CXF 提供的工具生成客户端代码。

  1. 在项目中添加 CXF 的依赖。
  2. 使用 CXF 提供的命令行工具生成客户端代码,命令如下:
wsdl2java -d  -p  

其中,为生成的代码存放的目录,为生成的代码所在的包名,为 WebService 的 WSDL 地址。

  1. 将生成的代码引入到项目中,然后就可以调用 WebService 接口了。例如:
HelloWorldService service = new HelloWorldService();
HelloWorldPortType port = service.getHelloWorldPort();
String result = port.sayHello("World");
System.out.println(result);

方式二:手动编写调用代码。

  1. 创建 JaxWsProxyFactoryBean 实例,并设置 WebService 的地址。
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress();
  1. 设置 WebService 接口的类。
factory.setServiceClass(HelloWorldPortType.class);
  1. 创建 WebService 接口的代理对象。
HelloWorldPortType port = (HelloWorldPortType) factory.create();
  1. 调用 WebService 接口的方法。
String result = port.sayHello("World");
System.out.println(result);

注意:以上代码中的 HelloWorldPortType 为 WebService 接口的类名。具体的类名需要根据生成的客户端代码或者 WebService 的定义进行调整。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-20发表,共计778字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)