新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ApacheCXF實戰(zhàn)之一:HelloWorldWebService
Apache的CXF現(xiàn)在幾乎成了Java領(lǐng)域構(gòu)建Web Service的***類庫,并且它也確實簡單易用,下面就通過幾篇系列文章做一下簡單介紹。

當然首先想到的當然還是那個Hello World示例。這個系列文章中用到的例子都是基于Maven構(gòu)建的工程,下面是我的pom.xml文件內(nèi)容
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0 com.googlecode.garbagecan.cxfstudy cxfstudy war 1.0-SNAPSHOT cxfstudy Maven Webapp http://maven.apache.org 2.2.7 org.apache.cxf cxf-rt-frontend-jaxws ${cxf.version} org.apache.cxf cxf-rt-transports-http ${cxf.version} org.apache.cxf cxf-rt-transports-http-jetty ${cxf.version} org.apache.cxf cxf-rt-ws-security ${cxf.version} org.apache.cxf cxf-rt-ws-policy ${cxf.version} org.apache.cxf cxf-bundle-jaxrs ${cxf.version} javax.ws.rs jsr311-api 1.1.1 org.slf4j slf4j-api 1.5.8 org.slf4j slf4j-jdk14 1.5.8 commons-httpclient commons-httpclient 3.0 commons-io commons-io 1.4 junit junit 4.8.1 test cxfstudy src/main/resources src/main/java ** **/*.java org.mortbay.jetty maven-jetty-plugin / 9000 org.apache.maven.plugins maven-compiler-plugin 1.5 1.5
#p#
下面來看看HelloWorld的具體例子。
1.創(chuàng)建HelloWorld 接口類
- 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.創(chuàng)建HelloWorld實現(xiàn)類
- package com.googlecode.garbagecan.cxfstudy.helloworld;
- public class HelloWorldImpl implements HelloWorld {
- public String sayHi(String name) {
- String msg = "Hello " + name + "!";
- return msg;
- }
- }
3.創(chuàng)建Server端測試類
- 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.創(chuàng)建Client端測試類
- 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。
原文鏈接:http://blog.csdn.net/kongxx/article/details/7525476
【系列文章】
- Apache CXF實戰(zhàn)之五:壓縮Web Service數(shù)據(jù)
- Apache CXF實戰(zhàn)之四:構(gòu)建RESTful Web Service
- Apache CXF實戰(zhàn)之三:傳輸Java對象
- Apache CXF實戰(zhàn)之二:集成Sping與Web容器
- Apache CXF實戰(zhàn)之一:Hello World Web Service
本文名稱:ApacheCXF實戰(zhàn)之一:HelloWorldWebService
標題網(wǎng)址:http://m.fisionsoft.com.cn/article/ccsghes.html


咨詢
建站咨詢
