Apache CXF实战之一 Hello World Web Service[转载] - 全栈笔记

Apache CXF实战之一 Hello World Web Service[转载]

任亦伟 JAVA 2015-03-09 108 次浏览 没有评论

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

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

001
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
002
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
003
    <modelVersion>4.0.0</modelVersion>
004
    <groupId>com.googlecode.garbagecan.cxfstudy</groupId>
005
    <artifactId>cxfstudy</artifactId>
006
    <packaging>war</packaging>
007
    <version>1.0-SNAPSHOT</version>
008
    <name>cxfstudy Maven Webapp</name>
009
    <url>http://maven.apache.org</url>
010
 
011
    <properties>
012
        <cxf.version>2.2.7</cxf.version>
013
    </properties>
014
 
015
    <dependencies>
016
        <dependency>
017
            <groupId>org.apache.cxf</groupId>
018
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
019
            <version>${cxf.version}</version>
020
        </dependency>
021
        <dependency>
022
            <groupId>org.apache.cxf</groupId>
023
            <artifactId>cxf-rt-transports-http</artifactId>
024
            <version>${cxf.version}</version>
025
        </dependency>
026
        <dependency>
027
            <groupId>org.apache.cxf</groupId>
028
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
029
            <version>${cxf.version}</version>
030
        </dependency>
031
        <dependency>
032
            <groupId>org.apache.cxf</groupId>
033
            <artifactId>cxf-rt-ws-security</artifactId>
034
            <version>${cxf.version}</version>
035
        </dependency>
036
        <dependency>
037
            <groupId>org.apache.cxf</groupId>
038
            <artifactId>cxf-rt-ws-policy</artifactId>
039
            <version>${cxf.version}</version>
040
        </dependency>
041
        <dependency>
042
            <groupId>org.apache.cxf</groupId>
043
            <artifactId>cxf-bundle-jaxrs</artifactId>
044
            <version>${cxf.version}</version>
045
        </dependency>
046
        <dependency>
047
            <groupId>javax.ws.rs</groupId>
048
            <artifactId>jsr311-api</artifactId>
049
            <version>1.1.1</version>
050
        </dependency>
051
        <dependency>
052
            <groupId>org.slf4j</groupId>
053
            <artifactId>slf4j-api</artifactId>
054
            <version>1.5.8</version>
055
        </dependency>
056
        <dependency>
057
            <groupId>org.slf4j</groupId>
058
            <artifactId>slf4j-jdk14</artifactId>
059
            <version>1.5.8</version>
060
        </dependency>
061
        <dependency>
062
            <groupId>commons-httpclient</groupId>
063
            <artifactId>commons-httpclient</artifactId>
064
            <version>3.0</version>
065
        </dependency>
066
        <dependency>
067
            <groupId>commons-io</groupId>
068
            <artifactId>commons-io</artifactId>
069
            <version>2.3</version>
070
        </dependency>
071
        <dependency>
072
            <groupId>junit</groupId>
073
            <artifactId>junit</artifactId>
074
            <version>4.8.1</version>
075
            <scope>test</scope>
076
        </dependency>
077
    </dependencies>
078
 
079
    <build>
080
        <finalName>cxfstudy</finalName>
081
        <resources>
082
            <resource>
083
                <directory>src/main/resources</directory>
084
            </resource>
085
            <resource>
086
                <directory>src/main/java</directory>
087
                <includes>
088
                    <include>**</include>
089
                </includes>
090
                <excludes>
091
                    <exclude>**/*.java</exclude>
092
                </excludes>
093
            </resource>
094
        </resources>
095
        <plugins>
096
            <plugin>
097
                <groupId>org.mortbay.jetty</groupId>
098
                <artifactId>maven-jetty-plugin</artifactId>
099
                <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 接口类

01
package com.googlecode.garbagecan.cxfstudy.helloworld;
02
 
03
import javax.jws.WebMethod;
04
import javax.jws.WebParam;
05
import javax.jws.WebResult;
06
import javax.jws.WebService;
07
 
08
@WebService
09
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端测试类

01
package com.googlecode.garbagecan.cxfstudy.helloworld;
02
 
03
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
04
 
05
// http://localhost:9000/HelloWorld?wsdl
06
public class Server {
07
    public static void main(String[] args) throws Exception {
08
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
09
        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
}
20
4.创建Client端测试类

01
package com.googlecode.garbagecan.cxfstudy.helloworld;
02
 
03
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
04
 
05
public class Client {
06
    public static void main(String[] args) {
07
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
08
        factory.setServiceClass(HelloWorld.class);
09
        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

发表评论

回顶部