當前位置:生活全書館 >

綜合知識

> 怎麼寫webservice

怎麼寫webservice

1. 如何寫一個簡單的web Service

本Guide利用Eclipse以及Ant建立一個簡單的Web Service,以演示Web Service的基本開發過程:1.系統條件:Eclipse Java EE IDE for Web Developers Java SE 6 Windows XP2.基本環境搭建:1)Java SE6 JDK的安裝:下載Java SE6 JDK,雙擊,安裝預設選項進行安裝即可。

怎麼寫webservice

2)Eclipse的安裝與配置: 安裝時直接解壓。 配置處有兩點,Window>Preferences>Java>Installed JREs確保如下設定: 安裝路徑可能略有不同。

Window>Preferences>Java>Compiler 確保如下設定:3.建立Server端工程和相關包與類:建立一個Java Project,命名為wsServerHelloWorld:在這個專案下建立包:org.gnuhpc.wsServer 在這個包下邊建立類:SayHello 在SayHello.java檔案中輸入以下程式碼:package org.gnuhpc.wsServer; import javax.jws.WebService; @WebService public class SayHello { private static final String SALUTATION = "Hello"; public String getGreeting(String name) { return SALUTATION + " " + name; } } 其中注意到@WebService ,這個稱作annotation或者metadata,Java SE 5中的Web Services Metadata Specification引入的。Java SE 6中對於Web Services規範的升級以及JAX-WS(Java API for XML Web Services)2.0規範,這些升級使得我們Web Services的建立和呼叫變得更加容易。

使用這些新功能,我們可以僅僅使用簡單的Annotations註釋從一個Java類建立Web Services。開發者將其類和方法之前用該annotations指定,類告訴runtime engine以Web Service的方式和操作來使能該類和方法。

這個annotations可以產生一個可佈署的Web Service,是一個WSDL對映annotations,將Java原始碼與代表Web Service的WSDL元素連線在了一起。4.使用Ant產生Server端程式碼:首先在專案中新建一個檔案:build.xml,然後使用OpenWith>AntEditor開啟,輸入以下指令碼程式碼: default指定了預設執行的Target為wsgen,wsgen可以建立一個能夠使用WebService的類,它生成所有用於WebService釋出的原始碼檔案和經過編譯過的二進位制類檔案。

它還生成WSDL和符合規範的該類的WebService。Target名稱為wsgen,具體執行的命令的引數:-cp 為類路徑-keep後產生的java檔案-s 產生的原始檔放在哪-d 產生的輸出問價放在哪 然後使用Ant Build選項執行:在成功執行的提示後,我們重新整理一下Project 我們在Project區域可以看到,結果如下:5.分佈Web Service org.gnuhpc.wsServer下建立一個類RunService:package org.gnuhpc.wsServer; import javax.xml.ws.Endpoint; public class RunService { /** *@paramargs */ public static void main(String[] args) { System.out.println("SayHello Web Service started."); Endpoint.publish("ponents = null; ///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { }/// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if(disposing && components != null) { components.Dispose(); } base.Dispose(disposing); } #endregion// 連線字串常量 const string szConn = "server=(local)taoyi;uid=sa;pwd=;" + "initial catalog=mydata;data source=taoyi";[。

3. webservice介面怎麼寫

eb廣泛用到的技術:

TCP/IP:通用網路協議,被各種裝置使用

HTML(標準通用標記語言下的一個應用):通用使用者介面,可以使用HTML標籤顯示資料

.NET: 不同應用程式間共享資料與資料交換

Java:寫一次可以在任何系統執行的通用程式語言,因為java具有跨平臺特性

XML(標準通用標記語言下的一個子集):通用資料表達語言,在web上傳送結構化資料的容易方法

他們的特點是其開放性,跨平臺性,開放性正是Web services的基礎。

4. webservice介面怎麼寫

以java為例:ava呼叫WebService可以直接使用Apache提供的axis.jar自己編寫程式碼,或者利用Eclipse自動生成WebService Client程式碼,利用其中的Proxy類進行呼叫。

理論上是一樣的,只不過用Eclipse自動生成程式碼省事些。1、編寫程式碼方式:package com.yudun.test;import java.rmi.RemoteException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis.message.PrefixedQName;import org.apache.axis.message.SOAPHeaderElement;import com.cezanne.golden.user.Exception;import com.cezanne.golden.user.UserManagerServiceProxy;import javax.xml.namespace.QName;import java.net.MalformedURLException;import javax.xml.rpc.ServiceException;import javax.xml.soap.Name;import javax.xml.soap.SOAPException;public class testWebService {public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException { //標識Web Service的具體路徑 String endpoint = "WebService服務地址"; // 建立 Service例項 Service service = new Service(); // 通過Service例項建立Call的例項 Call call = (Call) service.createCall(); //將Web Service的服務路徑加入到call例項之中. call.setTargetEndpointAddress( new java.net.URL(endpoint) );//為Call設定服務的位置 // 由於需要認證,故需要設定呼叫的SOAP頭資訊。

Name headerName = new PrefixedQName( new QName("釋出的wsdl裡的targetNamespace裡的url", "string_itemName") ); org.apache.axis.message.SOAPHeaderElement header = new SOAPHeaderElement(headerName); header.addTextNode( "blablabla" ); call.addHeader(header);// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("釋出的wsdl裡的targetNamespace裡的url", "SoapHeader"); // soapHeaderElement.setNamespaceURI("釋出的wsdl裡的targetNamespace裡的url"); // try // { // soapHeaderElement.addChildElement("string_itemName").setValue("blablabla"); // } // catch (SOAPException e) // { // e.printStackTrace(); // } // call.addHeader(soapHeaderElement); //呼叫Web Service的方法 org.apache.axis.description.OperationDesc oper; org.apache.axis.description.ParameterDesc param; oper = new org.apache.axis.description.OperationDesc(); oper.setName("opName"); param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName(".yudun.test;import java.rmi.RemoteException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis.message.PrefixedQName;import org.apache.axis.message.SOAPHeaderElement;import com.cezanne.golden.user.Exception;import com.cezanne.golden.user.UserManagerServiceProxy;import javax.xml.namespace.QName;import java.net.MalformedURLException;import javax.xml.rpc.ServiceException;import javax.xml.soap.Name;import javax.xml.soap.SOAPException;public class testWebService {public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException { //標識Web Service的具體路徑 String endpoint = "WebService服務地址"; // 建立 Service例項 Service service = new Service(); // 通過Service例項建立Call的例項 Call call = (Call) service.createCall(); //將Web Service的服務路徑加入到call例項之中. call.setTargetEndpointAddress( new java.net.URL(endpoint) );//為Call設定服務的位置 // 由於需要認證,故需要設定呼叫的SOAP頭資訊。

Name headerName = new PrefixedQName( new QName("釋出的wsdl裡的targetNamespace裡的url", "string_itemName") ); org.apache.axis.message.SOAPHeaderElement header = new SOAPHeaderElement(headerName); header.addTextNode( "blablabla" ); call.addHeader(header);// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("釋出的wsdl裡的targetNamespace裡的url", "SoapHeader"); // soapHeaderElement.setNamespaceURI("釋出的wsdl裡的targetNamespace裡的url"); // try // { // soapHeaderElement.addChildElement("string_itemName").setValue("blablabla"); // } // catch (SOAPException e) // { // e.printStackTrace(); // } // call.addHeader(soapHeaderElement); //呼叫Web Service的方法 org.apache.axis.description.OperationDesc oper; org.apache.axis.description.ParameterDesc param; oper = new org.apache.axis.description.OperationDesc(); oper.setName("opName"); param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false); param.setOmittable(true); oper.addParameter(param); param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg1"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false); param.setOmittable(true); oper.addParameter(param); param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg2"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false); param.setOmittable(true); oper.addParameter(param); oper.setReturnType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string")); oper.setReturnClass(java.lang.String.class); oper.setReturnQName(new javax.xml.namespace.QName("", "return")); oper.setStyle(org.apache.axis.constants.Style.WRAPPED); oper.setUse(org.apache.axis.constants.Use.LITERAL); oper.addFault(new org.apache.axis.description.FaultDesc( new javax.xml.namespace.QName("釋出的wsdl裡的targetNamespace裡的url", "Exception"), "Exception", new javax.xml.namespace.QName("釋出的wsdl裡的targetNamespace裡的url", "Exception"), true )); call.setOperation( oper ); call.setOperationName(new javax.xml.namespace.QName("釋出的wsdl裡的targetNamespace裡的url", "opName")); //呼叫Web Service,傳入引數 String res = ( String ) call.invoke( new Object[]("arg0","arg1")); System.out.println("==============="); return res; }/** * @param args */ public static void main(String[] args) { try { System.out.println(getResult()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } catch (SOAPException e) { e.printStackTrace(); } }}2、利用Eclipse自動生成WebService client程式碼就容易多了:(由於還不會發圖片,就用。

標籤: webservice
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zonghezhishi/ql141g.html