CXF WebService整合Spring

转自http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html

首先,CXF和spring整合需要准备如下jar包文件:

这边我是用Spring的jar包是Spring官方提供的,并没有使用CXF中的Spring的jar文件。

添加这么多文件后,首先在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>

然后在src目录中,新建一个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"/>

下面开始写服务器端代码,首先定制服务器端的接口,代码如下:

package com.hoo.service;
 
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.hoo.entity.User;
import com.hoo.entity.Users;
 
/**
 * <b>function:</b>定制客户端请求WebService所需要的接口
 * @author hoojo
 * @createDate 2011-3-18 上午08:22:55
 * @file ComplexUserService.java
 * @package com.hoo.service
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email [email protected]
 * @version 1.0
 */
@WebService
@SOAPBinding(style = Style.RPC)
public interface IComplexUserService {
    
    public User getUserByName(@WebParam(name = "name") String name);
    
    public void setUser(User user);
}

下面编写WebService的实现类,服务器端实现代码如下:

package com.hoo.service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.hoo.entity.User;
import com.hoo.entity.Users;
 
/**
 * <b>function:</b> WebService传递复杂对象,如JavaBean、Array、List、Map等
 * @author hoojo
 * @createDate 2011-3-18 上午08:22:55
 * @file ComplexUserService.java
 * @package com.hoo.service
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email [email protected]
 * @version 1.0
 */
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ComplexUserService implements IComplexUserService {
    
    public User getUserByName(@WebParam(name = "name") String name) {
        User user = new User();
        user.setId(new Date().getSeconds());
        user.setName(name);
        user.setAddress("china");
        user.setEmail(name + "@hoo.com");
        return user;
    }
    
    public void setUser(User user) {
        System.out.println("############Server setUser###########");
        System.out.println("setUser:" + user);
    }
}

注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。

下面要在applicationContext-server.xml文件中添加如下配置:

<bean id="userServiceBean" class="com.hoo.service.ComplexUserService"/>
 
<bean id="inMessageInterceptor" class="com.hoo.interceptor.MessageInterceptor">
    <constructor-arg  value="receive"/>
</bean>
 
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:server id="userService" serviceClass="com.hoo.service.IComplexUserService" address="/Users">
    <jaxws:serviceBean>
        <!-- 要暴露的 bean 的引用 -->
        <ref bean="userServiceBean"/>
    </jaxws:serviceBean>
    <jaxws:inInterceptors>
        <ref bean="inMessageInterceptor"/>
    </jaxws:inInterceptors>
    <jaxws:outInterceptors>
        <ref bean="outLoggingInterceptor"/>
    </jaxws:outInterceptors>
</jaxws:server>

下面启动tomcat服务器后,在WebBrowser中请求:

http://localhost:8080/CXFWebService/Users?wsdl

如果你能看到wsdl的xml文件的内容,就说明你成功了,注意的是上面地址的Users就是上面xml配置中的address的名称,是一一对应的。

下面编写客户端请求的代码,代码如下:

package com.hoo.client;
 
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.hoo.entity.User;
import com.hoo.service.IComplexUserService;
 
/**
 * <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 [email protected]
 * @version 1.0
 */
public class SpringUsersWsClient {
 
    public static void main(String[] args) {
        //调用WebService
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(IComplexUserService.class);
        factory.setAddress("http://localhost:8080/CXFWebService/Users");
        
        IComplexUserService service = (IComplexUserService) factory.create();
        
        System.out.println("#############Client getUserByName##############");
        User user = service.getUserByName("hoojo");
        System.out.println(user);
        
        user.setAddress("China-Guangzhou");
        service.setUser(user);
    }
}

运行后,可以在控制台中看到

log4j:WARN No appenders could be found for logger (org.apache.cxf.bus.spring.BusApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2011-3-28 18:12:26 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.hoo.com/}IComplexUserServiceService from class com.hoo.service.IComplexUserService
#############Client getUserByName##############
27#hoojo#[email protected]#china

Tomcat控制台
  

这个server端是通过Spring整合配置的,下面我们将Client端也通过Spring配置完成整合。

首先增加applicationContext-client.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="com.hoo.service.IComplexUserService" 
        address="http://localhost:8080/CXFWebService/Users"/>
</beans>

客户端请求代码如下:

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 com.hoo.service.IComplexUserService;
 
/**
 * <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 [email protected]
 * @version 1.0
 */
public class SpringUsersWsClient {
 
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
        
        IComplexUserService service = ctx.getBean("userWsClient", IComplexUserService.class);
        
        System.out.println("#############Client getUserByName##############");
        User user = service.getUserByName("hoojo");
        System.out.println(user);
        
        user.setAddress("China-Guangzhou");
        service.setUser(user);
    }
}

运行后结果如下:

#############Client getUserByName##############
45#hoojo#[email protected]#china
############Server setUser###########
setUser:45#hoojo#[email protected]#China-Guangzhou
时间: 2024-10-15 15:55:01

CXF WebService整合Spring的相关文章

CXF WebService整合SpringMVC的maven项目

首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html   http://blog.csdn.net/hu_shengyang/article/details/38384597 CXF是webService技术的一种实现工具,为什么用CXF来实现webService: 1.      Java的webService实现本身就是一个很耗性能的实现方案(xml与java对象之间在服务端以及客户端的互转比较消耗性能): 2.      目前java主

webservice整合spring cxf

下载cxf包,把他里面的包都添加进lib文件夹中. 创建一个接口.添加@WebService注解 @WebService public interface HelloWorld { String sayHi(@WebParam(name="text")String text); String sayHiToUser(User user); String[] SayHiToUserList(List<User> userList); } 创建接口的实现类,也添加@WebSer

Webservice整合Spring进行校验

服务端代码: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xm

webservice整合spring

接口HelloWorld需要添加webservice注解 package com.cs.webservice; import java.util.List; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(@WebParam(name="text")String text); String sayHiToUser(

cxf整合spring实现webservice

前面一篇文章中,webservice的服务端与客户端都是单独启动,但是在现实项目中,服务端单独启动太没有实际意义了,还是要整合框架启动,所以今天将记录如何整合spring框架. jar包下载地址如下: http://yun.baidu.com/share/link?shareid=547689626&uk=2836507213 (一).web.xml中添加spring与cxf的配置 <?xml version="1.0" encoding="UTF-8"

httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring)

原文:httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring) 源代码下载地址:httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring) 采用spring + cxf编写服务端 httpclient4编写客户端调用 如题,代码没有jar 完整包: 链接:http://pan.baidu.com/share/link?shareid=2162612373&uk=402880896 密

CXF整合Spring开发WebService

刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开环境变量配置窗口,点击新建,新建%CXF_HOME%变量,值为你下载的cxf所在的目录,我的是D:\tools\apache-cxf-3.1.0 2.在Path变量中新加一句%CXF_HOME%\lib,注意要和已有的path变量用;隔开 3.在CLASSPATH中新加一句%CXF_HOME%\li

WebService—CXF整合Spring实现接口发布和调用过程2

一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.18</version> </dependency> <de

WebService—CXF整合Spring实现接口发布和调用过程

一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.18</version> </dependency> <de