CXF和Spring MVC的整合
一.开发环境:
JDK:1.7
CXF:apache-cxf-3.0.9 , cxf:可以到http://cxf.apache.org/download.html下载
Spring: 3.2.4
二. CXF的jar包问题:
CXF的jar包有很多,其中很多都是用不到的,我开发的必备的jar包如下图:

apache-cxf-3.1.6 这个版本的 需要在 Spring 4.0以上的版本使用,我用spring 3.2 的版本,基本报错,网上说是 jar包版本的问题,姑且相信吧。
三. 新建项目工程
Server端
编写Webservice接口:
[html] 
<span style="font-size:18px;">package com.webservice;  
  
import javax.jws.WebService;  
  
/**   
 * <b>function:</b>定制客户端请求WebService所需要的接口   
 */    
@WebService  
public interface IUserService {    
   // public User getUserByName(@WebParam(name = "name") String name);    
    
    public String setUser(String name);    
} </span>接口实现:
[html] 
<span style="font-size:18px;">package com.webservice.webserviceimpl;  
  
import java.util.Date;  
  
import javax.jws.WebParam;  
import javax.jws.WebService;  
import javax.jws.soap.SOAPBinding;  
import javax.jws.soap.SOAPBinding.Style;  
  
import com.cosco.webservice.IUserService;  
import com.cosco.webservice.User;  
    
/**   
 * <b>function:</b> WebService传递复杂对象,如JavaBean、Array、List、Map等   
 */    
@WebService    
public class UserServiceImpl implements IUserService {    
   
    @Override  
    public String setUser(String name) {    
        System.out.println("############Server setUser###########");    
        System.out.println("setUser:" + name);    
        return "aaaa";  
    }  
  
}  </span>新建一个Webservice  的spring-webservice.xml:
[html] view plain copy <span style="font-size:18px;"><?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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- webservice配置文件 --> <import resource="classpath:META-INF/cxf/cxf.xml"/> <!-- 发布webservice接口 --> <bean id="userServiceBean" class="com.cosco.webservice.webserviceimpl.UserServiceImpl" /> <jaxws:server id="userService" serviceClass="com.cosco.webservice.IUserService" address="/Users"> <jaxws:serviceBean> <ref bean="userServiceBean" /> </jaxws:serviceBean> </jaxws:server> </beans> </span>
注意这里<beans>需要加入头部信息:
xmlns:jaxws="http://cxf.apache.org/jaxws" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
CXF 3.0之后去掉了其他的xml引入,只需引入即可:
[html] <span style="font-size:18px;"><import resource="classpath:META-INF/cxf/cxf.xml"/></span>
Web.xml 中加上:
[html] <span style="font-size:18px;"><?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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4"> <!-- 加载所有的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-*.xml</param-value> </context-param> <!-- 配置Spring监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置WebService --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app></span>
启动项目,浏览器中访问:
http://localhost:8080/TestWebservice/webservice/Users?wsdl

则说明发布成功。
四.客户端编写:
1.简单的测试:
[html] 
<span style="font-size:18px;">package com.webservice;  
  
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  
public class WebServiceClientTest {  
    public static void main(String[] args) throws Exception {   
        System.out.println(System.getProperty("java.endorsed.dirs"));  
          
        // 调用WebService    
        JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();  
        soapFactoryBean.setAddress("http://localhost:8080/TestWebservice/webservice/Users");  
        soapFactoryBean.setServiceClass(IUserService.class);  
        Object o = soapFactoryBean.create();  
        IUserService service = (IUserService)o;  
        String ss = service.setUser("aaa");  
        System.out.println(ss);  
    }   
}  
</span>如果报错的话,请参照:http://blog.csdn.net/feixueqianer/article/details/5850448
这篇文章进行修改,在JDK 下新建 endorsed文件夹,把JAXB-API-2.1.jar和jaxws-api.jar放入endorsed文件夹中。
2. Beans 访问:
首先在 XML 中加上:
[html] <span style="font-size:18px;"><?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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- webservice配置文件 --> <import resource="classpath:META-INF/cxf/cxf.xml"/> <!-- 调用webservice接口 --> <jaxws:client id="userWsClient" serviceClass="com.webservice.IUserService" address="http://localhost:8080/TestWebservice/webservice/Users"/> </beans> </span>
访问类:
[html] 
<span style="font-size:18px;">package com.webservice;  
  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class SpringUsersWsClient {  
  
    public static void main(String[] args) {    
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-webserviceclient.xml");    
        IUserService service = ctx.getBean("userWsClient", IUserService.class);    
        System.out.println("#############Client getUserByName##############");    
        String user = service.setUser("hoojo");    
        System.out.println(user);    
          
        String sss = service.setUser(user);    
          
        System.out.println(sss);    
    }    
      
}  
</span>3.spring mvc 中注入 bean:
在spring里能否方便的调用webService呢,CXF里提供了一个方便的工厂类,可以实现方便调用。
CXF中提供了生成webService客户端的工厂类org.apache.cxf.jaxws.JaxWsProxyFactoryBean,此类可以通过URL和stub接口类获取到stub代理(org.apache.cxf.jaxws.JaxWsProxyFactoryBean.create)。可以通过在Spring里将JaxWsProxyFactoryBean配置成bean,然后将接口类也配置成bean,但通过工厂方法来生成实例。配置如下:
[html] <span style="font-size:18px;"><?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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- webservice配置文件 --> <import resource="classpath:META-INF/cxf/cxf.xml"/> <!-- spring bean 调用方式 --> <bean id="iuserService" class="com.webservice.IUserService" factory-bean="subFactory" factory-method="create" /> <bean id="subFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.webservice.IUserService" /> <property name="address" value="http://localhost:8080/TestWebservice/webservice/Users" /> </bean> </beans> </span>
这个BEAN 就可以拿到, 这个bean注入到需要使用的bean里,就可以像调用本地bean一样透明的调用webService了。
重点是整合jar包版本,和 JAXB-API-2.1.jar和jaxws-api.jar 的问题。
转载自:http://blog.csdn.net/haitaofeiyang/article/details/51745063