cxf和spring的整合,访问webservice

1、需要jar包

apache-cxf-3.0.2

2、解压后放在某个盘符下。

如:E:\webservice\cxf\apache-cxf-3.0.2

3、提供wsdl文件(该文件由第三方提供:如service.wsdl)

4、进行cmd bin目录下E:\webservice\cxf\apache-cxf-3.0.2\bin

5、执行命令:wsdl2java.bat(将自动产生需要访问webservice的客服端代码)

6、配置spring(applicationContext-server.xml)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans >
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"

<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<jaxws:client id="userWsClient" serviceClass="org.tempuri.ServiceSoap"
address="http://localhost:8080/CXFWebService/Users"/>
</beans>


其中,org.tempuri.ServiceSoape是用wsdl2java.bat产生的代码。

7、配置web.xml

<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-server.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>


8、访问webservice(客服端代码)


package com.hoo.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hoo.entity.User;
import org.tempuri.ServiceSoap;

/**
* <b>function:</b>请求Spring整合CXF的WebService客户端
* @author hoojo
* @createDate 2011-3-28 下午03:20:35
* @file SpringUsersWsClient.java
* @package com.hoo.client
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class SpringUsersWsClient {

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");

org.tempuri.ServiceSoap service = ctx.getBean("userWsClient", ServiceSoap .class);

System.out.println("#############Client getUserByName##############");
User user = service.getUserByName("hoojo");
System.out.println(user);

user.setAddress("China-Guangzhou");
service.setUser(user);
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值