cxf實現webservice

  Apache CXF 是一個開放源代碼框架,提供了用於方便地構建和開發 Web 服務的可靠基礎架構。它容許建立高性能和可擴展的服務,您能夠將這樣的服務部署在 Tomcat 和基於 Spring 的輕量級容器中,以及部署在更高級的服務器上,例如 Jboss、IBM WebSphere 或 BEA WebLogic。html

簡單介紹入門教程,也爲本身記錄下。java

(1)下載cxf包,http://cxf.apache.org/download.html,我這裏用的是2.4.0的包web

        網盤地址以下:http://yun.baidu.com/share/link?shareid=564842495&uk=2836507213apache

        導入lib中的全部jar包,推薦使用library方式服務器

(2)編寫webservice接口類,接口實現類以下架構


接口須要指定annotation框架

@WebService
public interface IHello {
	public String sayHi(String name);
    
	public String printName(String name);
}


編寫上述接口的實現類,annotation指定了endpointInterface與serviceNameide

@WebService(endpointInterface="com.xj.service.IHello",serviceName="hello1Service")
public class HelloImpl implements IHello{

	@Override
	public String sayHi(String name) {
		System.out.println("hi,"+name);
		return "hi,"+name;
	}

	@Override
	public String printName(String name) {
		System.out.println("my name is,"+name);
		return "my name is,"+name;
	}

}

  

(3)編寫服務端,並啓動性能

public class RunServer {

	public static void main(String[] args) {
		IHello hello = new HelloImpl();
		Endpoint.publish("http://localhost/cxf/hello", hello);
		System.out.println("啓動server端");
	}

}

此處一樣採用的是endpoint來發布該服務,固然也能夠使用JaxWsServerFactoryBeanspa

運行main方法,訪問http://localhost/cxf/hello?wsdl 能夠看到該服務的wsdl文件

(4)編寫客戶端

public class RunClient {

	public static void main(String[] args) {
		JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean();
		proxy.setServiceClass(IHello.class);
		proxy.setAddress("http://localhost/cxf/hello?wsdl");
		IHello hello = (IHello)proxy.create();
		System.out.println(hello.sayHi("xiejun"));
		System.out.println(hello.printName("xiexie"));
	}

}

使用JaxWsProxyFactoryBean建立代理,指定service類,指定wsdl地址,

 調用代理類的create方法,便可訪問全部方法

相關文章
相關標籤/搜索