CXF调用webservices

如何实现调用Webservice

一、准备工作

1、按文档搭建新建平台项目,配置数据库
2
、确认pom中是否引入maven依赖,没有的话,请引入。

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>2.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>2.3.2</version>
</dependency>

 

 

二、实现步骤

 

(一)调用webservice

 

1如果调用的是由java发布的webservice,且能拿到接口文件,推荐使用这种方式调用

 

HelloWorldClient.java

不要忘记引入接口jar

 

1.package com.test.client;

2.

3.

4.import org.springframework.context.ApplicationContext;

5.import org.springframework.context.support.ClassPathXmlApplicationContext;

6.

7.

8.import com.test.server.IHelloWorldServer;

9.

10.

11.publicclass HelloWorldClient {

12.publicstaticvoid main(String[] args) {

13.

14. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");

15.//如果在项目中跑,这里可以自动注入,不用手动去取

16. IHelloWorldServer helloService = (IHelloWorldServer) context.getBean("client");

17. String response = helloService.sayHello("jialin");

18. System.out.println(response);

19. }

20.

21.

22.}

 


applicationContext-client.xml

 

[html]view plaincopyprint?

 

1.<?xmlversion="1.0"encoding="UTF-8"?>

2.<beansxmlns="http://www.springframework.org/schema/beans"

3.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4.xmlns:jaxws="http://cxf.apache.org/jaxws"

5.xsi:schemaLocation="http://www.springframework.org/schema/beans

6. http://www.springframework.org/schema/beans/spring-beans.xsd

7. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

8. <!--

9. ***注意***

10.手动添加的内容:

11.xmlns:jaxws="http://cxf.apache.org/jaxws"

12. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"

13. -->

14.

15.<importresource="classpath:META-INF/cxf/cxf.xml"/>

16.<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>

17.<importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>

18.

19.<beanid="client"class="com.ths.projects.ths.server.IHelloWorldServer "factory-bean="clientFactory"factory-method="create"/>

20.

21.<beanid="clientFactory"class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">

22.<propertyname="serviceClass"value="com.ths.projects.ths.server.IHelloWorldServer"/>

23.<propertyname="address"value="http://localhost:8080/ths/ws/helloService"/>

24.</bean>

25.</beans> 

 

2)没有接口文件,只能拿到wsdl,推荐使用以下方式调用。

 

1)依然使用CXF如果调用由java发布的webservice,推荐这种方式

 

A、通过wsdl文件生成客户端调用service的接口

 

B、生成方法:打开cxf的完整目录—》命令行中通过cd切换到这个目录(apache-cxf-2.3.2\bin)

 

—》执行命令》将生成的代码放入自己的项目中

 

wsdl2java http://localhost:8080/ths/ws/helloService?wsdl

 

:http://localhost:8080/ths/ws/helloService?wsdl换成相应的wsdl地址

 

C、通过以下方式调用远程方法

 

String address = "http://localhost:8080/ths/ws/helloService";

 

JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();

 

bean.setAddress(address);

 

bean.setServiceClass(IHelloWorldServer.class);

 

IHelloWorldServer ws = (IHelloWorldServer) bean.create();

 

System.out.println(ws.sayHello("贾琳"));

 

:要把IHelloWorldServer换成上一步中生成的service接口

 

2)使用axis调用webservice如果调用由.net发布的webservice,推荐这种方式

 

//不带?wsdl后缀

 

String endpoint = "http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx";

 

// 创建一个服务(service)调用(call)

 

Service service = new Service();

 

//通过service创建call对象

 

Call call = (Call) service.createCall();

 

// 设置service所在URL

 

call.setTargetEndpointAddress(new java.net.URL(endpoint));

 

//qqCheckOnline net 那边的方法 "http://WebXml.com.cn/" 这个也要注意Namespace的地址,不带也会报错

 

call.setOperationName(new QName("http://WebXml.com.cn/","qqCheckOnline"));

 

//qqCode也是.NET那边方法的参数名,即qqCheckOnline的参数名

 

call.addParameter(new QName("http://WebXml.com.cn/","qqCode"),

 

org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);

 

 

 

//避免Java调用.netwebService产生服务器未能识别 HTTP 标头 SOAPAction 的值错误

 

call.setUseSOAPAction(true);

 

call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING); //返回参数的类型

 

call.setSOAPActionURI("http://WebXml.com.cn/qqCheckOnline"); //这个也要注意就是要加上要调用的方法Add,不然也会报错

 

 

 

// Object 数组封装了参数

 

String ret = (String) call.invoke(new Object[] {"aaaaa"});

 

System.out.println("--------"+ret);

 

说明见注释,以上代码可在项目中直接使用。注意根据自己的情况修改相应的地址,命名空间,方法,参数

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值