一、CXF的介绍

Apache CXF是一个开源的WebService框架,CXF大大简化了Webservice的创建,同时它继承了XFire的传统,一样可以和spring天然的进行无缝的集成。CXF框架是一种基于servlet技术的SOA应用开发框架,要正常运用基于CXF应用框架开发的企业应用,除了CXF应用本身之外,还需要JDK和servlet容器的支持。

二、CXF的准备条件

所需要的jar包:

  xmlbeans-2.4.0.jar

  wss4j-1.5.9.jar

  jetty-server-7.1.6.v20100715.jar

  jetty-util-7.1.6.v20100715.jar

  geronimo-ws-metadata_2.0_spec-1.1.3.jar

  geronimo-activation_1.1_spec-1.1.jar

  geronimo-servlet_3.0_spec-1.0.jar

  velocity-1.6.4.jar

  jaxb-xjc-2.2.1.1.jar

  xml-resolver-1.2.jar

  wsdl4j-1.6.2.jar

  cxf-2.3.0.jar

  XmlSchema-1.4.7.jar

  jaxb-api-2.2.1.jar

  jaxb-impl-2.2.1.1.jar

  neethi-2.0.4.jar

  geronimo-annotation_1.0_spec-1.1.1.jar

  geronimo-stax-api_1.0_spec-1.0.1.jar

下载地址:http://download.csdn.net/detail/ch656409110/5748183   (取自己需要的jar包)


三、创建webservice服务端

1、先将jar包放入lib目录

2、在web.xml中配置CXF框架的核心servlet


[html]

  1. <!-- CXF -->  

  2. <servlet>    

  3.     <servlet-name>CXFServlet</servlet-name>    

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

  5.     <load-on-startup>1</load-on-startup>    

  6. </servlet>    

  7. <servlet-mapping>    

  8.     <servlet-name>CXFServlet</servlet-name>    

  9.     <url-pattern>/services/*</url-pattern>    

  10. </servlet-mapping>  

[html]

  1. <!-- CXF -->  

  2. <servlet>    

  3.     <servlet-name>CXFServlet</servlet-name>    

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

  5.     <load-on-startup>1</load-on-startup>    

  6. </servlet>    

  7. <servlet-mapping>    

  8.     <servlet-name>CXFServlet</servlet-name>    

  9.     <url-pattern>/services/*</url-pattern>    

  10. </servlet-mapping>  

	<!-- CXF -->
	<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>/services/*</url-pattern>  
	</servlet-mapping>


3、在applicationContext.xml中导入xml,并且发布webservice服务。



[html] 

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

  2. <beans  

  3.     xmlns="http://www.springframework.org/schema/beans"  

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

  5.     xmlns:p="http://www.springframework.org/schema/p"  

  6.     xmlns:tx="http://www.springframework.org/schema/tx"  

  7.     xmlns:aop="http://www.springframework.org/schema/aop"  

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

  9.     xmlns:jaxrs="http://cxf.apache.org/jaxrs"    

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

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

  12.                         http://www.springframework.org/schema/tx  

  13.                         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  

  14.                         http://www.springframework.org/schema/aop  

  15.                         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  

  16.                         http://cxf.apache.org/jaxws    

  17.                         http://cxf.apache.org/schemas/jaxws.xsd    

  18.                         http://cxf.apache.org/jaxrs    

  19.                         http://cxf.apache.org/schemas/jaxrs.xsd  

  20.                         "  

  21.                         default-autowire="byName"  

  22.                         >  

  23.     <import resource="classpath:META-INF/cxf/cxf.xml" />  

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

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

  26.       

  27.     <!-- <jaxws:endpoint id="facelookWebService" address="/facelookWebService" implementor="com.facelook.webservice.server.FacelookWebServiceImpl"></jaxws:endpoint> -->  

  28.     <!-- 不知道为什么,这里的webservice配置,只能用bean来实现,否则 注入的service为空。但是之前有个项目却可以,百思不得其解。。 -->  

  29.     <bean id="facelookWebService" class="com.facelook.webservice.server.FacelookWebServiceImpl"/>   

  30.     <jaxws:endpoint id="facelookWebService1" address="/facelookWebService" implementorClass="com.facelook.webservice.server.FacelookWebServiceImpl">  

  31.         <jaxws:implementor ref="facelookWebService"/>    

  32.     </jaxws:endpoint>  

  33. </beans>  

[html]

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

  2. <beans  

  3.     xmlns="http://www.springframework.org/schema/beans"  

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

  5.     xmlns:p="http://www.springframework.org/schema/p"  

  6.     xmlns:tx="http://www.springframework.org/schema/tx"  

  7.     xmlns:aop="http://www.springframework.org/schema/aop"  

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

  9.     xmlns:jaxrs="http://cxf.apache.org/jaxrs"    

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

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

  12.                         http://www.springframework.org/schema/tx  

  13.                         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  

  14.                         http://www.springframework.org/schema/aop  

  15.                         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  

  16.                         http://cxf.apache.org/jaxws    

  17.                         http://cxf.apache.org/schemas/jaxws.xsd    

  18.                         http://cxf.apache.org/jaxrs    

  19.                         http://cxf.apache.org/schemas/jaxrs.xsd  

  20.                         "  

  21.                         default-autowire="byName"  

  22.                         >  

  23.     <import resource="classpath:META-INF/cxf/cxf.xml" />  

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

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

  26.       

  27.     <!-- <jaxws:endpoint id="facelookWebService" address="/facelookWebService" implementor="com.facelook.webservice.server.FacelookWebServiceImpl"></jaxws:endpoint> -->  

  28.     <!-- 不知道为什么,这里的webservice配置,只能用bean来实现,否则 注入的service为空。但是之前有个项目却可以,百思不得其解。。 -->  

  29.     <bean id="facelookWebService" class="com.facelook.webservice.server.FacelookWebServiceImpl"/>   

  30.     <jaxws:endpoint id="facelookWebService1" address="/facelookWebService" implementorClass="com.facelook.webservice.server.FacelookWebServiceImpl">  

  31.         <jaxws:implementor ref="facelookWebService"/>    

  32.     </jaxws:endpoint>  

  33. </beans>  

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
						http://cxf.apache.org/jaxws  
					    http://cxf.apache.org/schemas/jaxws.xsd  
					    http://cxf.apache.org/jaxrs  
					    http://cxf.apache.org/schemas/jaxrs.xsd
					    "
						default-autowire="byName"
						>
    <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:endpoint id="facelookWebService" address="/facelookWebService" implementor="com.facelook.webservice.server.FacelookWebServiceImpl"></jaxws:endpoint> -->
    <!-- 不知道为什么,这里的webservice配置,只能用bean来实现,否则 注入的service为空。但是之前有个项目却可以,百思不得其解。。 -->
    <bean id="facelookWebService" class="com.facelook.webservice.server.FacelookWebServiceImpl"/> 
    <jaxws:endpoint id="facelookWebService1" address="/facelookWebService" implementorClass="com.facelook.webservice.server.FacelookWebServiceImpl">
    	<jaxws:implementor ref="facelookWebService"/>  
    </jaxws:endpoint>
</beans>


4、定义webservice接口FacelookWebService 和 实现类FacelookWebServiceImpl。



[java] 

  1. @WebService  

  2. public interface FacelookWebService {  

  3.       

  4.     /** 

  5.      * 根据传递的条件获取相册信息 

  6.      * xml的格式规范 

  7.      * <?xml version=\"1.0\" encoding=\"UTF-8\"?> 

  8.      * <facelook> 

  9.      *  <condition> 

  10.      *      <name></name> 

  11.      *      <description></description> 

  12.      *      <pageno></pageno> 

  13.      *      <pagesize></pagesize> 

  14.      *  </condition> 

  15.      * </facelook> 

  16.      * 这里的WebParam必须指定,否则调用的时候返回null 

  17.      * @return 

  18.      */  

  19.     public String getAlbumList(@WebParam(name="xmlStr") String xmlStr);  

  20. }  

  21.   

  22.   

  23. @WebService  

  24. //这后面的可以不写注释后面的配置,在applicationContext配置也一样(serviceName="facelookWebService",endpointInterface="com.facelook.webservice.server.FacelookWebService")  

  25. public class FacelookWebServiceImpl implements FacelookWebService{  

  26.   

  27.     @Autowired  

  28.     private AlbumService albumService;  

  29.       

  30.     @Override  

  31.     public String getAlbumList(String xmlStr) {  

  32.         try {  

  33.             List<Album> albumList = getAlbumPage(xmlStr);  

  34.             JSONArray jsonArray = JSONArray.fromObject(albumList);  

  35.             return jsonArray.toString();  

  36.         } catch (Exception e) {  

  37.             e.printStackTrace();  

  38.         }  

  39.         return null;  

  40.     }  

  41. }   

[java]

  1. @WebService  

  2. public interface FacelookWebService {  

  3.       

  4.     /** 

  5.      * 根据传递的条件获取相册信息 

  6.      * xml的格式规范 

  7.      * <?xml version=\"1.0\" encoding=\"UTF-8\"?> 

  8.      * <facelook> 

  9.      *  <condition> 

  10.      *      <name></name> 

  11.      *      <description></description> 

  12.      *      <pageno></pageno> 

  13.      *      <pagesize></pagesize> 

  14.      *  </condition> 

  15.      * </facelook> 

  16.      * 这里的WebParam必须指定,否则调用的时候返回null 

  17.      * @return 

  18.      */  

  19.     public String getAlbumList(@WebParam(name="xmlStr") String xmlStr);  

  20. }  

  21.   

  22.   

  23. @WebService  

  24. //这后面的可以不写注释后面的配置,在applicationContext配置也一样(serviceName="facelookWebService",endpointInterface="com.facelook.webservice.server.FacelookWebService")   

  25. public class FacelookWebServiceImpl implements FacelookWebService{  

  26.   

  27.     @Autowired  

  28.     private AlbumService albumService;  

  29.       

  30.     @Override  

  31.     public String getAlbumList(String xmlStr) {  

  32.         try {  

  33.             List<Album> albumList = getAlbumPage(xmlStr);  

  34.             JSONArray jsonArray = JSONArray.fromObject(albumList);  

  35.             return jsonArray.toString();  

  36.         } catch (Exception e) {  

  37.             e.printStackTrace();  

  38.         }  

  39.         return null;  

  40.     }  

  41. }   

@WebService
public interface FacelookWebService {
	
	/**
	 * 根据传递的条件获取相册信息
	 * xml的格式规范
	 * <?xml version=\"1.0\" encoding=\"UTF-8\"?>
	 * <facelook>
	 * 	<condition>
	 * 		<name></name>
	 * 		<description></description>
	 * 		<pageno></pageno>
	 * 		<pagesize></pagesize>
	 * 	</condition>
	 * </facelook>
	 * 这里的WebParam必须指定,否则调用的时候返回null
	 * @return
	 */
	public String getAlbumList(@WebParam(name="xmlStr") String xmlStr);
}


@WebService
//这后面的可以不写注释后面的配置,在applicationContext配置也一样(serviceName="facelookWebService",endpointInterface="com.facelook.webservice.server.FacelookWebService")
public class FacelookWebServiceImpl implements FacelookWebService{

    @Autowired
    private AlbumService albumService;
    
    @Override
    public String getAlbumList(String xmlStr) {
        try {
            List<Album> albumList = getAlbumPage(xmlStr);
            JSONArray jsonArray = JSONArray.fromObject(albumList);
            return jsonArray.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

这样子,基本上就可以了。


5、保存代码,发布项目,启动tomact。

在地址栏输入:http://localhost:8080/house/services/houseWebService?wsdl  即可看到发布的服务端的明细。

显示如下:


这就表示CXF发布的webservice服务端成功了。