Apache CXF实战之一 Hello World Web Service

本文链接:http://blog.csdn.net/kongxx/article/details/7525476

Apache的CXF现在几乎成了Java领域构建Web Service的首选类库,并且它也确实简单易用,下面就通过几篇系列文章做一下简单介绍。

当然首先想到的当然还是那个Hello World示例。这个系列文章中用到的例子都是基于Maven构建的工程,下面是我的pom.xml文件内容

  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>com.googlecode.garbagecan.cxfstudy</groupId>  
  5.     <artifactId>cxfstudy</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>1.0-SNAPSHOT</version>  
  8.     <name>cxfstudy Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.       
  11.     <properties>  
  12.         <cxf.version>2.2.7</cxf.version>  
  13.     </properties>  
  14.       
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>org.apache.cxf</groupId>  
  18.             <artifactId>cxf-rt-frontend-jaxws</artifactId>  
  19.             <version>${cxf.version}</version>  
  20.         </dependency>  
  21.         <dependency>  
  22.             <groupId>org.apache.cxf</groupId>  
  23.             <artifactId>cxf-rt-transports-http</artifactId>  
  24.             <version>${cxf.version}</version>  
  25.         </dependency>  
  26.         <dependency>  
  27.             <groupId>org.apache.cxf</groupId>  
  28.             <artifactId>cxf-rt-transports-http-jetty</artifactId>  
  29.             <version>${cxf.version}</version>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.apache.cxf</groupId>  
  33.             <artifactId>cxf-rt-ws-security</artifactId>  
  34.             <version>${cxf.version}</version>  
  35.         </dependency>  
  36.         <dependency>  
  37.             <groupId>org.apache.cxf</groupId>  
  38.             <artifactId>cxf-rt-ws-policy</artifactId>  
  39.             <version>${cxf.version}</version>  
  40.         </dependency>  
  41.         <dependency>  
  42.             <groupId>org.apache.cxf</groupId>  
  43.             <artifactId>cxf-bundle-jaxrs</artifactId>  
  44.             <version>${cxf.version}</version>  
  45.         </dependency>  
  46.         <dependency>  
  47.             <groupId>javax.ws.rs</groupId>  
  48.             <artifactId>jsr311-api</artifactId>  
  49.             <version>1.1.1</version>  
  50.         </dependency>  
  51.         <dependency>  
  52.             <groupId>org.slf4j</groupId>  
  53.             <artifactId>slf4j-api</artifactId>  
  54.             <version>1.5.8</version>  
  55.         </dependency>  
  56.         <dependency>  
  57.             <groupId>org.slf4j</groupId>  
  58.             <artifactId>slf4j-jdk14</artifactId>  
  59.             <version>1.5.8</version>  
  60.         </dependency>  
  61.         <dependency>  
  62.             <groupId>commons-httpclient</groupId>  
  63.             <artifactId>commons-httpclient</artifactId>  
  64.             <version>3.0</version>  
  65.         </dependency>  
  66.         <dependency>  
  67.             <groupId>commons-io</groupId>  
  68.             <artifactId>commons-io</artifactId>  
  69.             <version>2.3</version>  
  70.         </dependency>  
  71.         <dependency>  
  72.             <groupId>junit</groupId>  
  73.             <artifactId>junit</artifactId>  
  74.             <version>4.8.1</version>  
  75.             <scope>test</scope>  
  76.         </dependency>  
  77.     </dependencies>  
  78.       
  79.     <build>  
  80.         <finalName>cxfstudy</finalName>  
  81.         <resources>  
  82.             <resource>  
  83.                 <directory>src/main/resources</directory>  
  84.             </resource>  
  85.             <resource>  
  86.                 <directory>src/main/java</directory>  
  87.                 <includes>  
  88.                     <include>**</include>  
  89.                 </includes>  
  90.                 <excludes>  
  91.                     <exclude>**/*.java</exclude>  
  92.                 </excludes>  
  93.             </resource>  
  94.         </resources>  
  95.         <plugins>  
  96.             <plugin>  
  97.                 <groupId>org.mortbay.jetty</groupId>  
  98.                 <artifactId>maven-jetty-plugin</artifactId>  
  99.                 <configuration>  
  100.                     <contextPath>/</contextPath>  
  101.                     <connectors>  
  102.                         <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
  103.                             <port>9000</port>  
  104.                         </connector>  
  105.                     </connectors>  
  106.                 </configuration>  
  107.             </plugin>  
  108.             <plugin>  
  109.                 <groupId>org.apache.maven.plugins</groupId>  
  110.                 <artifactId>maven-compiler-plugin</artifactId>  
  111.                 <configuration>  
  112.                     <source>1.5</source>  
  113.                     <target>1.5</target>  
  114.                 </configuration>  
  115.             </plugin>  
  116.         </plugins>  
  117.     </build>  
  118.   
  119. </project>  
下面来看看HelloWorld的具体例子。

1.创建HelloWorld 接口类

  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebResult;  
  6. import javax.jws.WebService;  
  7.   
  8. @WebService  
  9. public interface HelloWorld {  
  10.     @WebMethod  
  11.     @WebResult String sayHi(@WebParam String text);  
  12. }  
2.创建HelloWorld实现类 
  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.   
  3. public class HelloWorldImpl implements HelloWorld {  
  4.   
  5.     public String sayHi(String name) {  
  6.         String msg = "Hello " + name + "!";  
  7.         return msg;  
  8.     }  
  9. }  
3.创建Server端测试类
  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  4.   
  5. // http://localhost:9000/HelloWorld?wsdl  
  6. public class Server {  
  7.     public static void main(String[] args) throws Exception {  
  8.         JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
  9.         factory.setServiceClass(HelloWorldImpl.class);  
  10.           
  11.         factory.setAddress("http://localhost:9000/ws/HelloWorld");  
  12.         factory.create();  
  13.   
  14.         System.out.println("Server start...");  
  15.         Thread.sleep(60 * 1000);  
  16.         System.out.println("Server exit...");  
  17.         System.exit(0);  
  18.     }  
  19. }  
4.创建Client端测试类
  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4.   
  5. public class Client {  
  6.     public static void main(String[] args) {  
  7.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  8.         factory.setServiceClass(HelloWorld.class);  
  9.         factory.setAddress("http://localhost:9000/ws/HelloWorld");  
  10.         HelloWorld helloworld = (HelloWorld) factory.create();  
  11.         System.out.println(helloworld.sayHi("kongxx"));  
  12.         System.exit(0);  
  13.     }  
  14. }  
5.测试

首先运行Server类来启动Web Service服务,然后访问http://localhost:9000/ws/HelloWorld?wsdl地址来确定web service启动正确。

运行Client测试类,会在命令行输出Hello kongxx!的message。



转载自:http://blog.csdn.net/kongxx/article/details/7525476

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值