Apache CXF 2.7与Spring 3集成

来源:互联网 发布:淘宝买家旺旺 编辑:程序博客网 时间:2024/05/21 12:35

做项目时,涉及到多个异构系统的交互,采用了Apache CXF,下面通过实例演示Apache CXF 2.7与spring集成,发布和调用Web Service。


环境:

win8 64位

tomcat 7 64位

jdk 7 64位

cxf 2.7

spring 3


项目结构

1、在同一个项目中模仿服务端和客户端(实际项目中是不可能的,但是客户端却依赖于服务端的Web Service接口,那么可以通过导出jar的方式



类库



在apache cxf 官网下载cxf2.7,解压,在lib下即可找到这些jar


几个类的代码

[java] view plain copy
 print?
  1. package com.test.server;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5.   
  6. @WebService  
  7. public interface IHelloWorldServer {  
  8.     public String sayHello(String username);  
  9. }  

[java] view plain copy
 print?
  1. package com.test.server;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. @WebService(endpointInterface = "com.test.server.IHelloWorldServer", serviceName = "HelloService")  
  6. public class HelloWorldServerImp implements IHelloWorldServer{  
  7.   
  8.     @Override  
  9.     public String sayHello(String username) {  
  10.           
  11.         return username+" : HelloWorld";  
  12.     }  
  13.       
  14. }  

[java] view plain copy
 print?
  1. package com.test.client;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import com.test.server.IHelloWorldServer;  
  7.   
  8. public class HelloWorldClient {  
  9.     public static void main(String[] args) {  
  10.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
  11.         IHelloWorldServer helloService = (IHelloWorldServer) context.getBean("client");  
  12.         String response = helloService.sayHello("jialin");  
  13.         System.out.println(response);  
  14.     }  
  15.   
  16. }  

相应的配置文件

applicationContext-server.xml

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                        http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.                        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  8.     <!--   
  9.         ***注意***   
  10.         手动添加的内容:  
  11.         xmlns:jaxws="http://cxf.apache.org/jaxws"  
  12.         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"  
  13.      -->  
  14.       
  15.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  16.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  17.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  18.   
  19.     <jaxws:endpoint id="helloService" implementor="com.test.server.HelloWorldServerImp" address="/helloService" />  
  20.           
  21. </beans>  

applicationContext-client.xml

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                        http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.                        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  8.     <!--   
  9.         ***注意***   
  10.         手动添加的内容:  
  11.         xmlns:jaxws="http://cxf.apache.org/jaxws"  
  12.         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"  
  13.      -->  
  14.       
  15.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  16.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  17.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  18.   
  19.     <bean id="client" class="com.test.server.IHelloWorldServer" factory-bean="clientFactory" factory-method="create" />  
  20.   
  21.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
  22.         <property name="serviceClass" value="com.test.server.IHelloWorldServer" />  
  23.         <property name="address" value="http://localhost:8080/cxf_demo/ws/helloService" />  
  24.     </bean>  
  25. </beans>  

web.xml

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.   <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  4.   <display-name>cxf_demo</display-name>  
  5.     
  6.   <context-param>  
  7.       <param-name>contextConfigLocation</param-name>  
  8.       <param-value>classpath:applicationContext-server.xml</param-value>  
  9.   </context-param>  
  10.    
  11.   <listener>  
  12.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  13.   </listener>  
  14.     
  15.   <servlet>  
  16.     <servlet-name>CXFServlet</servlet-name>  
  17.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  18.     <load-on-startup>1</load-on-startup>  
  19.   </servlet>  
  20.     
  21.   <servlet-mapping>  
  22.     <servlet-name>CXFServlet</servlet-name>  
  23.     <url-pattern>/ws/*</url-pattern>  
  24.   </servlet-mapping>  
  25.   <welcome-file-list>  
  26.     <welcome-file>index.jsp</welcome-file>  
  27.   </welcome-file-list>  
  28. </web-app>  

配置完成后,部署到tomcat,成功启动后。


在浏览器中输入

http://localhost:8080/cxf_demo/ws


说明web service发布成功,可以点击进入查看相应的WSDL.


这时运行客户端得到如下结果



以上是在同一个项目中模仿客户端和服务端。下面再看一个服务和客户端不在同一个项目中的,这是符合现实的。


2、

服务端

与上述项目结构一样,只是没有com.test.client这个包和applicationContext-client.xml


客户端

新建一个普通的Java项目即可,项目结构:



注意cxf-serverinterface这个jar,是到处的服务端的接口即为com.test.server.IHelloWorld


HelloWorldClient.java

[java] view plain copy
 print?
  1. package com.test.client;  
  2.   
  3.   
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7.   
  8. import com.test.server.IHelloWorldServer;  
  9.   
  10.   
  11. public class HelloWorldClient {  
  12.     public static void main(String[] args) {  
  13.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
  14.         IHelloWorldServer helloService = (IHelloWorldServer) context.getBean("client");  
  15.         String response = helloService.sayHello("jialin");  
  16.         System.out.println(response);  
  17.     }  
  18.   
  19.   
  20. }  

applicationContext-client.xml

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                        http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.                        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  8.     <!--   
  9.         ***注意***   
  10.         手动添加的内容:  
  11.         xmlns:jaxws="http://cxf.apache.org/jaxws"  
  12.         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"  
  13.      -->  
  14.       
  15.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  16.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  17.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  18.   
  19.     <bean id="client" class="com.test.server.IHelloWorldServer" factory-bean="clientFactory" factory-method="create" />  
  20.   
  21.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
  22.         <property name="serviceClass" value="com.test.server.IHelloWorldServer" />  
  23.         <property name="address" value="http://localhost:8080/cxf_demo/ws/helloService" />  
  24.     </bean>  
  25. </beans>  

测试:


部署服务端到tomcat,启动。

启动Web Service服务,然后访问http://localhost:8080/cxf_demo/ws地址来确定web service启动正确。

运行Client测试类,运行结果:




成功!

0 0
原创粉丝点击