01_CXF之HelloWorld

1.下载CXF

    http://apache.etoak.com//cxf/2.3.0/apache-cxf-2.3.0.zip 

   CXF是XFire的升级半,XFire已经停止更新了

    让后将lib下所有jar包(可能有些jar包不需要,还没研究)添加到BuildPath下

 

2.编写接口

   写道

@WebService
public interface IHello {

public String sayHello(String word);
}

 @WebService 注解IHello发布成服务,同样其实现类也要添加

   写道

@WebService
public class HelloImpl implements IHello {

public String sayHello(String word) {
System.out.println("receive client word"+word);

return "Good Luck!Client,"+word;
}

}

 

3.通过CXF内置Jetty Servelet容器发布服务

   写道

public class MainServer {

public static void main(String[] args) {

JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();

factory.setServiceClass(HelloImpl.class);
factory.setAddress("http://localhost:8080/helloword");

Server server = factory.create();
server.start();

}

}

 JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();---webService工厂类

 factory.setServiceClass(HelloImpl.class);---一定是实现类,要不然客户端找不到的

 factory.setAddress("http://localhost:8080/helloword");---通过该地址我们就可以访问我们服务

 http://localhost:8080/helloword?wsdl 录入地址栏,回车,你就可以看到wsdl描述

 

4.客户端

   客户端需要IHello接口,实际开发时,我们不需要关心我们需要什么接口,只要我们拿到WSDL文件,就可以把wsdl转换成代码(接口会自动生成)

public class Client {

	public static void main(String[] args) {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress("http://localhost:8080/helloword");
		factory.setServiceClass(IHello.class);//绑定接口类
		
		IHello hello = (IHello)factory.create();
		System.out.println(hello.sayHello("How are you!"));
	}

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值