文章出處
文章列表
Apache CXF實現WebService非常簡單實用,只需要幾步就可以實現一個簡單的web service。
首先我們需要新建一個maven項目,在pom中添加依賴和jetty作為測試的web service的web容器。
如下是測試用到的pom文件內容:
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.outofmemory</groupId> <artifactId>hello-apache-cxf</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hello-apache-cxf</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>hello-apache-cxf</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>
然后需要定義web service接口,在接口定義中要添加必要的annotation注解來標注出來webservice接口和提供的方法,以及參數等,如下接口文件:
package cn.outofmemory.hello.apache.cxf; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService public interface HelloService { @WebMethod @WebResult String hello(@WebParam String who); }
定義完接口之后需要實現接口,接口實現代碼如下:
package cn.outofmemory.hello.apache.cxf; public class SimpleHelloService implements HelloService { public String hello(String who) { return "hello " + who; } }
這個實現類不需要做任何的標注。
這樣webservice的實現部分就算完了,我們需要在web容器中運行web service,如下Server代碼:
package cn.outofmemory.hello.apache.cxf; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class Server { public static void main(String[] args) throws Exception { JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); factory.setServiceClass(SimpleHelloService.class); factory.setAddress("http://localhost:9000/ws/HelloService"); factory.create(); System.out.println("Server start..."); } }
可以運行這個類,然后在瀏覽器中訪問:http://localhost:9000/ws/HelloService。
可以讓Server端保持啟動狀態,下面我們寫Client端來調用server端的webservice,如下client端代碼:
package cn.outofmemory.hello.apache.cxf; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class ServiceClient { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(HelloService.class); factory.setAddress("http://localhost:9000/ws/HelloService"); HelloService helloworld = (HelloService) factory.create(); System.out.println(helloworld.hello("outofmemory.cn")); System.exit(0); } }
運行client,可以得到hello outofmemory.cn
的輸出。
相關:
WebService學習總結(三)——使用JDK開發WebService
簡單的WebService實現
文章列表
全站熱搜