cxf小实例

来源:互联网 发布:人工智能论文话题英文 编辑:程序博客网 时间:2024/06/05 03:26

使用MyEclipse 创建Web  Project ,添加  maven 支持。 生成项目结构如下:

 

 

2、POM.XML 文件内容如下 。(包含  cxf 与 spring 等的jar包)

 

Java代码  收藏代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>cxf</groupId>  
  5.     <artifactId>cxf</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>cxf Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.   
  11.   
  12.     <properties>  
  13.         <cxf.version>2.4.3</cxf.version>  
  14.         <spring.version>2.5.6</spring.version>  
  15.     </properties>  
  16.     <dependencies>  
  17.         <dependency>  
  18.             <groupId>org.apache.cxf</groupId>  
  19.             <artifactId>cxf-rt-frontend-jaxws</artifactId>  
  20.             <version>${cxf.version}</version>  
  21.         </dependency>  
  22.         <dependency>  
  23.             <groupId>org.apache.cxf</groupId>  
  24.             <artifactId>cxf-rt-transports-http</artifactId>  
  25.             <version>${cxf.version}</version>  
  26.         </dependency>  
  27.         <!-- Jetty is needed if you're are not using the CXFServlet -->  
  28.         <dependency>  
  29.             <groupId>org.apache.cxf</groupId>  
  30.             <artifactId>cxf-rt-transports-http-jetty</artifactId>  
  31.             <version>${cxf.version}</version>  
  32.         </dependency>  
  33.         <dependency>  
  34.             <groupId>org.apache.cxf</groupId>  
  35.             <artifactId>cxf-tools-common</artifactId>  
  36.             <version>${cxf.version}</version>  
  37.         </dependency>  
  38.         <dependency>  
  39.             <groupId>org.apache.cxf</groupId>  
  40.             <artifactId>cxf-tools-java2ws</artifactId>  
  41.             <version>${cxf.version}</version>  
  42.         </dependency>  
  43.         <dependency>  
  44.             <groupId>org.apache.cxf</groupId>  
  45.             <artifactId>cxf-tools-validator</artifactId>  
  46.             <version>${cxf.version}</version>  
  47.         </dependency>  
  48.         <dependency>  
  49.             <groupId>org.apache.cxf</groupId>  
  50.             <artifactId>cxf-tools-wsdlto-core</artifactId>  
  51.             <version>${cxf.version}</version>  
  52.         </dependency>  
  53.         <dependency>  
  54.             <groupId>org.apache.cxf</groupId>  
  55.             <artifactId>cxf-tools-wsdlto-databinding-jaxb</artifactId>  
  56.             <version>${cxf.version}</version>  
  57.         </dependency>  
  58.         <dependency>  
  59.             <groupId>org.springframework</groupId>  
  60.             <artifactId>spring-webmvc</artifactId>  
  61.             <version>${spring.version}</version>  
  62.             <type>jar</type>  
  63.             <scope>compile</scope>  
  64.             <exclusions>  
  65.                 <exclusion>  
  66.                     <artifactId>spring-web</artifactId>  
  67.                     <groupId>org.springframework</groupId>  
  68.                 </exclusion>  
  69.             </exclusions>  
  70.         </dependency>  
  71.         <dependency>  
  72.             <groupId>org.springframework</groupId>  
  73.             <artifactId>spring</artifactId>  
  74.             <version>${spring.version}</version>  
  75.             <type>jar</type>  
  76.             <scope>compile</scope>  
  77.             <exclusions>  
  78.                 <exclusion>  
  79.                     <artifactId>commons-logging</artifactId>  
  80.                     <groupId>commons-logging</groupId>  
  81.                 </exclusion>  
  82.             </exclusions>  
  83.         </dependency>  
  84.         <dependency>  
  85.             <groupId>org.apache.cxf</groupId>  
  86.             <artifactId>cxf-tools-wsdlto-frontend-jaxws</artifactId>  
  87.             <version>${cxf.version}</version>  
  88.         </dependency>  
  89.     </dependencies>  
  90.     <repositories>  
  91.   
  92.         <repository>  
  93.             <id>apache-snapshots</id>  
  94.             <name>Apache SNAPSHOT Repository</name>  
  95.             <url>http://repository.apache.org/snapshots/</url>  
  96.             <snapshots>  
  97.                 <enabled>true</enabled>  
  98.             </snapshots>  
  99.         </repository>  
  100.         <repository>  
  101.             <id>atlassian-m2-repository</id>  
  102.             <url>https://m2proxy.atlassian.com/repository/public</url>  
  103.         </repository>  
  104.     </repositories>  
  105.   
  106.     <build>  
  107.         <plugins>  
  108.             <plugin>  
  109.                 <groupId>org.apache.cxf</groupId>  
  110.                 <artifactId>cxf-java2ws-plugin</artifactId>  
  111.                 <version>${cxf.version}</version>  
  112.             </plugin>  
  113.         </plugins>  
  114.     </build>  
  115. </project>  

 3、创建服务接口类与实现类。(JDK 默认是1.6 u13 的,最好更换为 1.6 u 17以上)

 

Java代码  收藏代码
  1. package demo;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5. import javax.jws.soap.SOAPBinding;  
  6. import javax.jws.soap.SOAPBinding.Style;  
  7.   
  8. @WebService  
  9. @SOAPBinding(style = Style.RPC)  
  10. public interface IHelloWorld {  
  11.       
  12.     public String sayHello(@WebParam(name = "name") String name);  
  13.   
  14. }  

 package demo;

Java代码  收藏代码
  1. public class HelloWorldImpl implements IHelloWorld{  
  2.   
  3.     public String sayHello(String name) {  
  4.          return name + " say: Hello World!";  
  5.     }  
  6. }  
 

 

 4、Spring 配置文件

 

Java代码  收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"   
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  4.     xsi:schemaLocation="  
  5.         http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.         http://cxf.apache.org/jaxws   
  8.         http://cxf.apache.org/schemas/jaxws.xsd">  
  9.  <!-- 上面  1、  http://cxf.apache.org/schemas/jaxws.xsd   
  10.            2、 http://cxf.apache.org/jaxws    
  11.            3、http://cxf.apache.org/schemas/jaxws.xsd  
  12.        为引入 CXF 的命名空间 -->        
  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. <!-- 上面3个 import 为导入  CXF 的 Bean 定义文件-->  
  19.   
  20.   
  21.   
  22. <!-- 定义具体实现的 Bean ,这个 Bean 的定义与 Spring 普通的 Bean 定义是一样的 -->  
  23.     <bean id="helloService" class="demo.HelloWorldImpl" />  
  24.   
  25.   
  26. <!-- 通过 CXF 提供的 <jaxws:server> 标签,将定义的 Bean 暴露出去成为 Web Service 服务 -->  
  27.  <!-- serviceClass = 接口类 -->  
  28.  <!-- address = 调用时的URL -->  
  29.     <jaxws:server id="helloWebService"    
  30.         serviceClass="demo.IHelloWorld"    
  31.         address="/helloWorld">    
  32.         <!-- 要暴露的 bean 的引用,上面定义的bean id -->  
  33.         <jaxws:serviceBean>        
  34.             <ref bean="helloService"/>    
  35.         </jaxws:serviceBean>  
  36.     </jaxws:server>  
  37.     
  38.   
  39. </beans>  
 

5、WEB.XML 文件

 

 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <display-name></display-name>   
  8.     
  9.     <!-- 我们的示例是需要通过 Servlet 容器进行服务暴露,  
  10.                         因此需要配置相对应的 web.xml 文件,  
  11.                         首先是增加 Spring 的配置文件加载 Listene -->  
  12.     <context-param>  
  13.         <param-name>contextConfigLocation</param-name>  
  14.         <param-value>/Cxf-config.xml</param-value>  
  15.     </context-param>  
  16.     <!-- Spring ContextLoaderListener -->  
  17.     <listener>  
  18.         <listener-class>  
  19.             org.springframework.web.context.ContextLoaderListener  
  20.         </listener-class>  
  21.     </listener>  
  22.     
  23.     <!-- CXF Servlet 的定义,以及它的映射 -->  
  24.     <servlet>  
  25.         <servlet-name>CXFServlet</servlet-name>  
  26.         <servlet-class>  
  27.             org.apache.cxf.transport.servlet.CXFServlet  
  28.         </servlet-class>  
  29.         <load-on-startup>1</load-on-startup>  
  30.     </servlet>  
  31.     <!-- CXFServlet Mapping -->  
  32.     <servlet-mapping>  
  33.         <servlet-name>CXFServlet</servlet-name>  
  34.         <url-pattern>/*</url-pattern>  
  35.     </servlet-mapping>  
  36.     
  37.   <welcome-file-list>  
  38.     <welcome-file>index.jsp</welcome-file>  
  39.   </welcome-file-list>  
  40. </web-app>  
 

6、发布并访问 http://localhost:9098/mycxf/helloWorld?wsdl 。

 

7、客户端Spring调用测试

 

8、客户端 Spring 文件 

 

 

Java代码  收藏代码
  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="  
  6.         http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.         http://cxf.apache.org/jaxws   
  9.         http://cxf.apache.org/schemas/jaxws.xsd">  
  10.           
  11.     <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  12.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
  13.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
  14.   
  15.   
  16.     <!-- 客户端  -->  
  17.     <jaxws:client id="helloServiceClient"   
  18.         serviceClass="demo.IHelloWorld"   
  19.         address="http://localhost:9098/mycxf/helloWorld"/>  
  20.           
  21. </beans>  

 9、客户端测试代码

 

 

Java代码  收藏代码
  1. package client;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import demo.IHelloWorld;  
  7.   
  8. /** 
  9.  * 客户端调用,测试代码 
  10.  */  
  11. public class HelloServiceClient {  
  12.     public static void main(String[] args) {  
  13.         // 加载客户端的配置定义  
  14.         ApplicationContext context = new ClassPathXmlApplicationContext("Cxf-client.xml");  
  15.         // 获取定义的 Web Service Bean  
  16.         IHelloWorld helloService = (IHelloWorld) context.getBean("helloServiceClient");  
  17.         String username = "Ailan";  
  18.         // 调用方法进行服务消费  
  19.         String result = helloService.sayHello(username);  
  20.         System.out.println("Result:" + result);  
  21.     }  
  22. }  

 10、最终项目结构

11、附件中为完整代码。

0 0
原创粉丝点击