CXF与Spring集成Web Service

  1. 使用MyEclipse8.5 首先创建一个Web工程,加入Spring框架支持。
  2. 选择Spring3.0的版本,并且加入Spring3.0 Core包 和 Spring3.0 Web 2个包。
  3. 加入CXF的jar包
  4. 开始编写服务端的服务接口以及接口实现类。
  5. 编写服务接口以及接口实现类是,在类前加上@Web Service  标注。注明此接口为Web Service的接口。
  6. 编写完成之后,需要在Spring配置文件中加入CXF框架使用到的命名空间。
    <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:jaxws="http://cxf.apache.org/jaxws"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://cxf.apache.org/jaxws 
      	http://cxf.apache.org/schemas/jaxws.xsd">
    </beans>
     
  7. 然后继续在配置文件中定义一个JAX-WS的端点 。
    <?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:jaxws="http://cxf.apache.org/jaxws"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://cxf.apache.org/jaxws 
      	http://cxf.apache.org/schemas/jaxws.xsd">
    
    	<jaxws:endpoint id="Math" implementor="com.xwj.service.MathServiceImpl"
    		address="/math">
    	</jaxws:endpoint>
    
    </beans>
     
  8. implementor 是实现类,address是用于提供服务的访问地址。
  9. 在web.xml加入配置,使用web加载Spring容器的配置监听器。
  10. 加入CXF servlet的相关配置。
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<!-- 设置Spring容器加载配置文件路径 -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:applicationContext*.xml</param-value>
    	</context-param>
    
    	<!-- CXF相关配置 -->
    	<servlet>
    		<servlet-name>CXFServlet</servlet-name>
    		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    	</servlet>
    	
    	<servlet-mapping>
    		<servlet-name>CXFServlet</servlet-name>
    		<url-pattern>/*</url-pattern>
    	</servlet-mapping>
    
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    </web-app>
     
  11. 部署tomcat并启动
  12. 信息: Creating Service {http://service.xwj.com/}MathServiceImplService from class com.xwj.service.MathService  则显示成功
  13. http://localhost:8888/WSServer/math?wsdl   查看 WSDL是否成功
  14. 可以在MyEclipse Launch SAOP 游览器中 点击WSDL page -- WSDL main 输入WSDL地址
  15. 找到服务方法测试。
  16. 以上都是服务器端的配置。接下来是客户端。
  17. 首先也是创建Java工程,加入Spring框架,加入Spring3.0 Core包 Spring3.0 AOP  Spring3.0 Web 3个包。AOP必须加入
  18. 导入CXF框架的jar文件,在spring导入命名空间。
  19. 创建一个服务的接口并且加入@WebService 的注释。
  20. 配置客户端
    <?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:jaxws="http://cxf.apache.org/jaxws"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://cxf.apache.org/jaxws 
      	http://cxf.apache.org/schemas/jaxws.xsd">
      	
      	<jaxws:client id="client" serviceClass="com.xwj.client.MathClient"
      		address="http://localhost:8888/WSServer/math"
      	></jaxws:client>
    </beans>
     
  21. 创建Client类。使用getBean()方法获取服务接口实例,并调用其方法,运行。

猜你喜欢

转载自348292700.iteye.com/blog/1986658