最近由于項目需要,一直在學(xué)習(xí)OSGI,在學(xué)習(xí)OSGI的這段時間內(nèi),不斷的接觸到apache的一些優(yōu)秀的開源項目,比如說Felix、CXF等。Felix是Apache對OSGI R4規(guī)范的一個輕量級實現(xiàn)。你使用eclipse創(chuàng)建的plugin(插件)工程都是可以正常運行在Felix中的。前提是你創(chuàng)建bundle的時候選擇標準選項這一欄。好了本篇文章主要是用來介紹CXF的,關(guān)于Felix就不再深入討論了,有興趣的可以自行去研究下。
關(guān)于CXF,不做過多的解釋。官方的解釋已經(jīng)夠清楚了。相信大家之前在Java環(huán)境下創(chuàng)建webservice程序大多數(shù)選擇的是xfire這個框架吧。后來好多專家不再推薦這個東東。都建議使用CXF。在未接觸到CXF之前,本人一向喜歡用xfire這個框架來創(chuàng)建自己的webservice。還了,廢話不多說,先來看個HelloWorld的程序,教大家快速上手。
首先去Apache網(wǎng)站下載CXF所需要的jar,我本人下載是apache-cxf-2.2.10.zip這個包。這里為了方便期間創(chuàng)建一個java工程。啊?java工程,這有點不可思議了,不是要創(chuàng)建webservice嗎?怎么是java工程?呵呵,這里就是CXF的神奇之處!
添加必須的jar到你的classpath路徑下。
cxf-2.2.10.jar 核心jar
jetty-6.1.21.jar 用來啟動jetty服務(wù)器
jetty-util-6.1.21.jar jetty輔助工具
wsdl4j-1.6.2.jar wsdl支持工具
XmlSchema-1.4.5.jar
這就是CXF的最小配置,以上jar包缺一不可
創(chuàng)建一個接口
在瀏覽器中輸入http://localhost:8080/hello?wsdl,即可看到wsdl文件了。其中http://localhost:8080/hello部分為代碼里指定的Address。
wsdl文件信息:
關(guān)于CXF,不做過多的解釋。官方的解釋已經(jīng)夠清楚了。相信大家之前在Java環(huán)境下創(chuàng)建webservice程序大多數(shù)選擇的是xfire這個框架吧。后來好多專家不再推薦這個東東。都建議使用CXF。在未接觸到CXF之前,本人一向喜歡用xfire這個框架來創(chuàng)建自己的webservice。還了,廢話不多說,先來看個HelloWorld的程序,教大家快速上手。
首先去Apache網(wǎng)站下載CXF所需要的jar,我本人下載是apache-cxf-2.2.10.zip這個包。這里為了方便期間創(chuàng)建一個java工程。啊?java工程,這有點不可思議了,不是要創(chuàng)建webservice嗎?怎么是java工程?呵呵,這里就是CXF的神奇之處!
添加必須的jar到你的classpath路徑下。
cxf-2.2.10.jar 核心jar
jetty-6.1.21.jar 用來啟動jetty服務(wù)器
jetty-util-6.1.21.jar jetty輔助工具
wsdl4j-1.6.2.jar wsdl支持工具
XmlSchema-1.4.5.jar
這就是CXF的最小配置,以上jar包缺一不可
創(chuàng)建一個接口
package com.cxf.service;
public interface HelloWorldCxfService {
String sayHello(String username);
}
創(chuàng)建該接口的實現(xiàn)類public interface HelloWorldCxfService {
String sayHello(String username);
}
package com.cxf.service;
public class HelloWorldCxfServiceImpl implements HelloWorldCxfService {
public String sayHello(String username) {
return "Hello,"+username;
}
}
發(fā)布webservicepublic class HelloWorldCxfServiceImpl implements HelloWorldCxfService {
public String sayHello(String username) {
return "Hello,"+username;
}
}
package com.cxf.server;
import org.apache.cxf.frontend.ServerFactoryBean;
import com.cxf.service.HelloWorldCxfService;
import com.cxf.service.HelloWorldCxfServiceImpl;
public class Server {
public static void main(String[] args){
HelloWorldCxfServiceImpl worldCxfServiceImpl=new HelloWorldCxfServiceImpl();
ServerFactoryBean factoryBean=new ServerFactoryBean();
factoryBean.setAddress("http://localhost:8080/hello");
factoryBean.setServiceClass(HelloWorldCxfService.class);
factoryBean.setServiceBean(worldCxfServiceImpl);
factoryBean.create();
}
}
運行Server,注意不要關(guān)閉,在控制臺會打印如下信息:import org.apache.cxf.frontend.ServerFactoryBean;
import com.cxf.service.HelloWorldCxfService;
import com.cxf.service.HelloWorldCxfServiceImpl;
public class Server {
public static void main(String[] args){
HelloWorldCxfServiceImpl worldCxfServiceImpl=new HelloWorldCxfServiceImpl();
ServerFactoryBean factoryBean=new ServerFactoryBean();
factoryBean.setAddress("http://localhost:8080/hello");
factoryBean.setServiceClass(HelloWorldCxfService.class);
factoryBean.setServiceBean(worldCxfServiceImpl);
factoryBean.create();
}
}
2010-9-10 9:44:16 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
2010-9-10 9:44:16 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8080/hello
2010-09-10 09:44:16.296::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
2010-09-10 09:44:16.296::INFO: jetty-6.1.21
2010-09-10 09:44:16.390::INFO: Started SelectChannelConnector@localhost:8080
客戶端調(diào)用信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
2010-9-10 9:44:16 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8080/hello
2010-09-10 09:44:16.296::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
2010-09-10 09:44:16.296::INFO: jetty-6.1.21
2010-09-10 09:44:16.390::INFO: Started SelectChannelConnector@localhost:8080
package com.cxf.server;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import com.cxf.service.HelloWorldCxfService;
public class Client {
public static void main(String[] args) {
ClientProxyFactoryBean factoryBean=new ClientProxyFactoryBean();
factoryBean.setAddress("http://localhost:8080/hello");
factoryBean.setServiceClass(HelloWorldCxfService.class);
HelloWorldCxfService worldCxfService=(HelloWorldCxfService) factoryBean.create();
System.out.println(worldCxfService.sayHello("張三"));
}
}
運行Client代碼,控制臺打印如下信息:import org.apache.cxf.frontend.ClientProxyFactoryBean;
import com.cxf.service.HelloWorldCxfService;
public class Client {
public static void main(String[] args) {
ClientProxyFactoryBean factoryBean=new ClientProxyFactoryBean();
factoryBean.setAddress("http://localhost:8080/hello");
factoryBean.setServiceClass(HelloWorldCxfService.class);
HelloWorldCxfService worldCxfService=(HelloWorldCxfService) factoryBean.create();
System.out.println(worldCxfService.sayHello("張三"));
}
}
2010-9-10 9:46:58 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
Hello,張三
到此,我們的webservice,已經(jīng)成功調(diào)用了。大家是不是迫不及待的想看下wsdl文件是啥樣的呢?信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
Hello,張三
在瀏覽器中輸入http://localhost:8080/hello?wsdl,即可看到wsdl文件了。其中http://localhost:8080/hello部分為代碼里指定的Address。
wsdl文件信息:
<?xml version="1.0" ?>
- <wsdl:definitions name="HelloWorldCxfService" targetNamespace="http://service.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://service.cxf.com/" xmlns:tns="http://service.cxf.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="sayHello" type="tns:sayHello" />
- <xsd:complexType name="sayHello">
- <xsd:sequence>
<xsd:element minOccurs="0" name="arg0" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse" />
- <xsd:complexType name="sayHelloResponse">
- <xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
- <wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters" />
</wsdl:message>
- <wsdl:portType name="HelloWorldCxfServicePortType">
- <wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello" />
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloWorldCxfServiceSoapBinding" type="tns:HelloWorldCxfServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="sayHello">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloWorldCxfService">
- <wsdl:port binding="tns:HelloWorldCxfServiceSoapBinding" name="HelloWorldCxfServicePort">
<soap:address location="http://localhost:8080/hello" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
- <wsdl:definitions name="HelloWorldCxfService" targetNamespace="http://service.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://service.cxf.com/" xmlns:tns="http://service.cxf.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="sayHello" type="tns:sayHello" />
- <xsd:complexType name="sayHello">
- <xsd:sequence>
<xsd:element minOccurs="0" name="arg0" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse" />
- <xsd:complexType name="sayHelloResponse">
- <xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
- <wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters" />
</wsdl:message>
- <wsdl:portType name="HelloWorldCxfServicePortType">
- <wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello" />
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloWorldCxfServiceSoapBinding" type="tns:HelloWorldCxfServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="sayHello">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloWorldCxfService">
- <wsdl:port binding="tns:HelloWorldCxfServiceSoapBinding" name="HelloWorldCxfServicePort">
<soap:address location="http://localhost:8080/hello" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>