Apache的CXF现在几乎成了Java领域构建Web Service的***类库,并且它也确实简单易用,下面就通过几篇系列文章做一下简单介绍。
当然首先想到的当然还是那个Hello World示例。这个系列文章中用到的例子都是基于Maven构建的工程,下面是我的pom.xml文件内容
- 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
#p#
下面来看看HelloWorld的具体例子。
1.创建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.创建HelloWorld实现类
- package com.googlecode.garbagecan.cxfstudy.helloworld;
- public class HelloWorldImpl implements HelloWorld {
- public String sayHi(String name) {
- String msg = "Hello " + name + "!";
- return msg;
- }
- }
3.创建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.创建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
【系列文章】
分享名称:ApacheCXF实战之一:HelloWorldWebService
分享路径:http://www.shufengxianlan.com/qtweb/news5/420405.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联