微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

五、CXF WebService整合Spring

五、CXF WebService整合Spring

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

image

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

添加这么多文件后,首先在web.xml中添加如下配置:

<!-- 加载spring容器配置 -->
<listener>
    listener-class>org.springframework.web.context.ContextLoaderListener</>
<!-- 设置spring容器加载配置文件路径 -->
context-paramparam-name>contextConfigLocationparam-value>classpath*:applicationContext-server.xml>
 
>org.springframework.web.util.IntrospectorCleanupListenerservletservlet-name>CXFServiceservlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-mappingurl-pattern>/*>

然后在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"/>
="classpath:meta-inf/cxf/cxf-extension-soap.xml"="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);
    
    void setUser(User user);
}

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

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
 * <b>function:</b> WebService传递复杂对象,如JavaBean、Array、List、Map等
 */
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
class ComplexUserService implements IComplexUserService {
    
    "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;
    }
    
    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"/>
 
="inMessageInterceptor" ="com.hoo.interceptor.MessageInterceptor"constructor-arg  value="receive"bean="outLoggingInterceptor" ="org.apache.cxf.interceptor.LoggingOutInterceptor"<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
jaxws:server ="userService" serviceClass="com.hoo.service.IComplexUserService" address="/Users"jaxws:serviceBean>
        <!-- 要暴露的 bean 的引用 -->
        ref ="userServiceBean"/>
    jaxws:inInterceptors="inMessageInterceptor"jaxws:outInterceptors="outLoggingInterceptor"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.service.IComplexUserService;
 
 * <b>function:</b>请求Spring整合CXF的WebService客户端
 * @createDate 2011-3-28 下午03:20:35
 * @file SpringUsersWsClient.java
 * @package com.hoo.client
 */
class SpringUsersWsClient {
 
    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控制台
 

image

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

首先增加applicationContext-client.xml配置文件文件内容如下:

="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    >
    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"
    
    />
    
    jaxws:client ="userWsClient" ="com.hoo.service.IComplexUserService" 
        ="http://localhost:8080/CXFWebService/Users"beans>

客户端请求代码如下:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClasspathXmlApplicationContext;
void main(String[] args) {
        ApplicationContext ctx = new ClasspathXmlApplicationContext("applicationContext-client.xml");
        
        IComplexUserService service = ctx.getBean("userWsClient",IComplexUserService.class);
        
        System.out.println("China-Guangzhou");
        service.setUser(user);
    }
}

运行后结果如下:

#############Client getUserByName##############
45#hoojo#[email protected]#china
############Server setUser###########
setUser:45#hoojo#[email protected]#China-Guangzhou

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐