480
技術社區[雲棲]
關於基於xfire webservice框架開發webservice的總結
關於基於xfire webservice框架開發webservice的總結
基礎條件:依賴包和插件
開發環境:myeclipse7.5 +tomcat6.0
插件:xfire插件
包括服務器端的開發和客戶端的調用代碼的開發
一、 服務器端開發
1. 開發服務接口
package com.mybank.xfire.example;
public interface IBankingService {
public String transferFunds(String fromAccount, String toAccount,
double amount, String currency);
}
2. 開發服務接口實現類
package com.mybank.xfire.example;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class BankingService implements IBankingService {
//Default constructor.
public BankingService(){
}
/** Transfers fund from one account to another.
*/
public String transferFunds(
String fromAccount, String toAccount, double amount, String currency){
String statusMessage = "";
//調用業務邏輯執行操作.
//建立並返回狀態信息.
try {
NumberFormat formatter = new DecimalFormat("###,###,###,###.00");
statusMessage = "COMPLETED: " + currency + " " + formatter.format(amount)+
" was successfully transferred from A/C# " + fromAccount + " to A/C# " + toAccount;
} catch (Exception e){
statusMessage = "BankingService.transferFunds(): EXCEPTION: " + e.toString();
}
return statusMessage;
}
}
3. 接口和實現類的配置
配置文件中加入實現類和接口的配置
配置文件名稱為services.xml,其位置為WEB-INF/classes/META-INF/xfire/services.xml
<beans xmlns="https://xfire.codehaus.org/config/1.0">
<service>
<name>Banking</name>
<namespace>mybank</namespace>
<serviceClass>com.mybank.xfire.example.IBankingService</serviceClass>
<implementationClass>com.mybank.xfire.example.BankingService</implementationClass>
</service>
</beans>
注意:<name>Banking</name> 這個Banking
4. web.xml中配置xfire的servlet.
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="https://java.sun.com/xml/ns/javaee"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://java.sun.com/xml/ns/javaee
https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
注意:servlet配置和url映射
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
5. 發布後測試:
https://192.168.63.187:8080/webservice/services/Banking?wsdl
返回如下:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="mybank" xmlns:soapenc12="https://www.w3.org/2003/05/soap-encoding" xmlns:tns="mybank" xmlns:wsdl="https://schemas.xmlsoap.org/wsdl/" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:soap11="https://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="https://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="https://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="https://www.w3.org/2003/05/soap-envelope">
<wsdl:types>
<xsd:schema xmlns:xsd="https://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="mybank">
<xsd:element name="transferFunds">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="1" name="in1" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="1" name="in2" type="xsd:double"/>
<xsd:element maxOccurs="1" minOccurs="1" name="in3" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="transferFundsResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="transferFundsResponse">
<wsdl:part name="parameters" element="tns:transferFundsResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="transferFundsRequest">
<wsdl:part name="parameters" element="tns:transferFunds">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="BankingPortType">
<wsdl:operation name="transferFunds">
<wsdl:input name="transferFundsRequest" message="tns:transferFundsRequest">
</wsdl:input>
<wsdl:output name="transferFundsResponse" message="tns:transferFundsResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BankingHttpBinding" type="tns:BankingPortType">
<wsdlsoap:binding transport="https://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="transferFunds">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="transferFundsRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="transferFundsResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Banking">
<wsdl:port name="BankingHttpPort" binding="tns:BankingHttpBinding">
<wsdlsoap:address location="https://192.168.63.187:8080/webservice/services/Banking"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
則表示發布成功。
至此,服務器端開發全部完成。
二、客戶端調用代碼的開發
包括以下幾種方式
1. 如果能夠知道並得到service的類,那麼可以在客戶端中通過xfier的代理工廠生成service類。然後調用相應的方法。(有服務的代碼)
package com.mybank.xfire.example;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
Client c=new Client();
String ret;
try {
ret = c.callWebService("111", "222", 1.1d, "aaaaa");
System.out.println("s="+ret);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String callWebService(
String fromAccount, String toAccount, double amount, String currency)
throws MalformedURLException, Exception {
//Create a metadata of the service 創建一個service的元數據
Service serviceModel = new ObjectServiceFactory().create(IBankingService.class);
System.out.println("callSoapServiceLocal(): got service model." );
//Create a proxy for the deployed service 為XFire獲得一個代理工廠對象
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
//得到一個服務的本地代理
String serviceUrl = "https://192.168.63.187:8080/webservice/services/Banking";
IBankingService client = null;
try {
client = (IBankingService) factory.create(serviceModel, serviceUrl);
} catch (MalformedURLException e) {
System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString());
}
//Invoke the service 調用服務 返回狀態結果信息
String serviceResponse = "";
try {
serviceResponse = client.transferFunds(fromAccount, toAccount, amount, currency);
} catch (Exception e){
System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString());
serviceResponse = e.toString();
}
System.out.println("WsClient.callWebService(): status=" + serviceResponse);
//Return the response
return serviceResponse;
}
}
2. 沒有服務的代碼,則可以通過xfire插件,自動生成相關的客戶端代碼
插件的地址和安裝
利用xfire生成web服務客戶端的方法有多種,Eclipse Plugin為XFire的WSDL->Code generator提供了Eclipse支持,它需要Eclipse 3.2和Java 5。這裏我們用Eclipse Plugin根據wsdl文件地址生成客戶端代碼,而我們隻需要編寫幾行代碼即可實現調用web服務。下麵就是方法步驟:
第一步,安裝插件。
打開Eclipse的Help菜單,選擇”Software Updates”,然後再選擇”Find and Install.”
選擇"Search for new features to install",然後點擊Next
選擇"Create New Remote Site", 在name中輸入"XFire",在eclipse update site中輸入https://dist.codehaus.org/xfire/update/
選擇OK
選擇Finish。
注意完成上述操作後,eclipse要下載和安裝插件,時間長短要視網速而定,請耐心等待,安裝完成時還要重啟eclipse。
第二步,使用插件。
首先新建一個java項目XFireProject,然後依次選擇菜單File->New->Other ,選擇XFire文件夾下的Code generation from WSDL document,打開代碼生成向導.
輸入wsdl 地址,選擇 生成類所在類的包,執行可自動生成一係列文件。其中有XXXXXXclient的類即可調用代碼,
package com.yanek.test;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
BankingClient bc=new BankingClient();
System.out.println("xxxyyyy="+bc.getBankingHttpPort().transferFunds("aaa", "bbb", 1.0d, "11111111"));
}
}
其中BankingClient為自動生成的代碼
https://192.168.63.187:8080/webservice/services/Banking?wsdl
需要導入xfire相關jar和apache comm包。
3. 三,由於service類在很多情況下並不是隻有自己開發的。這時候很有可能你沒有辦法得到service類,但是service發布的wsdl文件是可以得到,xfire可以通過wsdl生成client。這裏又包含兩種方式,第一,把wsdl下載下來到本地的classpath。通過讀取wsdl生成。或者,可以直接通過url來生成。這裏說明第二種情況
https://192.168.63.187:8080/webservice/services/Banking?wsdl
package com.yane.test;
import java.net.HttpURLConnection;
import java.net.URL;
import org.codehaus.xfire.client.Client;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Test test=new Test();
String a=test.transferFunds("111", "222", 1.1d, "aaaaa");
System.out.println("xxxxxxx="+a);
}
public String transferFunds(
String fromAccount, String toAccount, double amount, String currency)
{
try{
String wsdl = "https://192.168.63.187:8080/webservice/services/Banking?wsdl";
URL url = new URL(wsdl);
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.connect();
Client client = new Client(httpConnection.getInputStream(),null);
Object []results = client.invoke("transferFunds", new Object[]{fromAccount,toAccount,amount,currency});
return (String)results[0];
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
其中,
Object []results = client.invoke("transferFunds", new Object[]{fromAccount,toAccount,amount,currency}
中transferFunds為方法名, new Object[]{fromAccount,toAccount,amount,currency} 為對象參數值
最後更新:2017-04-02 06:51:36