使用Apache CXF创建简单Web Service

1.创建HelloWorld 接口类

package com.googlecode.garbagecan.cxfstudy.helloworld; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService public interface HelloWorld { public @WebResult(name="String")String sayHi(@WebParam(name="text") String text); }

2.创建HelloWorld实现类

package com.googlecode.garbagecan.cxfstudy.helloworld; public class HelloWorldImpl implements HelloWorld { public String sayHi(String name) { String msg = "Hello " + name + "!"; System.out.println("Server: " + msg); 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 { HelloWorld helloWorld = new HelloWorldImpl(); JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:9000/HelloWorld"); factory.setServiceBean(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/HelloWorld"); HelloWorld helloworld = (HelloWorld) factory.create(); System.out.println(helloworld.sayHi("kongxx")); System.exit(0); } }

5.测试

首先运行Server类来启动Web Service服务,然后访问http://localhost:9000/HelloWorld?wsdl地址来确定web service启动正确。
运行Client测试类,会在命令行输出Hello kongxx!的message。

 

 

 

转载于:https://www.cnblogs.com/wdpp/archive/2010/05/05/2386346.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值