Apache CXF实战之一 Hello World Web Service
Apache的CXF现在几乎成了Java领域构建Web Service的首选类库,并且它也确实简单易用,下面就通过几篇系列文章做一下简单介绍。
当然首先想到的当然还是那个Hello World示例。这个系列文章中用到的例子都是基于Maven构建的工程,下面是我的pom.xml文件内容
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("xx")); System.exit(0); } }
5.测试
首先运行Server类来启动Web Service服务,然后访问http://localhost:9000/ws/HelloWorld?wsdl地址来确定web service启动正确。
运行Client测试类,会在命令行输出Hello xx!的message。
相关推荐
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03
likesyour 2020-08-01