Apache CXF实战之一 Hello World Web Service

23 篇文章 1 订阅
8 篇文章 0 订阅

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

</project>
下面来看看HelloWorld的具体例子。

1.创建HelloWorld 接口类

  1. package com.googlecode.garbagecan.cxfstudy.helloworld;
  2. import javax.jws.WebMethod;
  3. import javax.jws.WebParam;
  4. import javax.jws.WebResult;
  5. import javax.jws.WebService;
  6. @WebService
  7. public interface HelloWorld {
  8. @WebMethod
  9. @WebResult String sayHi(@WebParam String text);
  10. }
package com.googlecode.garbagecan.cxfstudy.helloworld;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
    @WebMethod
    @WebResult String sayHi(@WebParam String text);
}
2.创建HelloWorld实现类

  1. package com.googlecode.garbagecan.cxfstudy.helloworld;
  2. public class HelloWorldImpl implements HelloWorld {
  3. public String sayHi(String name) {
  4. String msg = "Hello " + name + "!";
  5. return msg;
  6. }
  7. }
package com.googlecode.garbagecan.cxfstudy.helloworld;

public class HelloWorldImpl implements HelloWorld {

    public String sayHi(String name) {
        String msg = "Hello " + name + "!";
        return msg;
    }
}
3.创建Server端测试类

  1. package com.googlecode.garbagecan.cxfstudy.helloworld;
  2. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
  3. // http://localhost:9000/HelloWorld?wsdl
  4. public class Server {
  5. public static void main(String[] args) throws Exception {
  6. JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
  7. factory.setServiceClass(HelloWorldImpl.class);
  8. factory.setAddress("http://localhost:9000/ws/HelloWorld");
  9. factory.create();
  10. System.out.println("Server start...");
  11. Thread.sleep(60 * 1000);
  12. System.out.println("Server exit...");
  13. System.exit(0);
  14. }
  15. }
package com.googlecode.garbagecan.cxfstudy.helloworld;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

// http://localhost:9000/HelloWorld?wsdl
public class Server {
    public static void main(String[] args) throws Exception {
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        factory.setServiceClass(HelloWorldImpl.class);
        
        factory.setAddress("http://localhost:9000/ws/HelloWorld");
        factory.create();

        System.out.println("Server start...");
        Thread.sleep(60 * 1000);
        System.out.println("Server exit...");
        System.exit(0);
    }
}
4.创建Client端测试类

  1. package com.googlecode.garbagecan.cxfstudy.helloworld;
  2. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  3. public class Client {
  4. public static void main(String[] args) {
  5. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  6. factory.setServiceClass(HelloWorld.class);
  7. factory.setAddress("http://localhost:9000/ws/HelloWorld");
  8. HelloWorld helloworld = (HelloWorld) factory.create();
  9. System.out.println(helloworld.sayHi("kongxx"));
  10. System.exit(0);
  11. }
  12. }
package com.googlecode.garbagecan.cxfstudy.helloworld;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {
    public static void main(String[] args) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(HelloWorld.class);
        factory.setAddress("http://localhost:9000/ws/HelloWorld");
        HelloWorld helloworld = (HelloWorld) factory.create();
        System.out.println(helloworld.sayHi("kongxx"));
        System.exit(0);
    }
}
5.测试

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值