文章來(lái)源:http://dev.csdn.net/author/fuwei2241/cb5b26ad75fa41829d967fbefef3685f.html
本人通過(guò)實(shí)際項(xiàng)目開(kāi)發(fā),敘述關(guān)于測(cè)試的使用和感悟,本人堅(jiān)持用代碼說(shuō)話的方式,來(lái)講述測(cè)試在項(xiàng)目中的真正實(shí)際應(yīng)用,同時(shí)也希望大家多多賜教,共同探討,總結(jié)更適用的開(kāi)發(fā)經(jīng)驗(yàn),共同分享!
文章來(lái)源:http://dev.csdn.net/author/fuwei2241/cb5b26ad75fa41829d967fbefef3685f.html jdk為1。3版本,沒(méi)有replaceAll()方法 /** } if (returnString != null && returnString.length() > stringLength) { for (int i = 0; i < sourceString.length(); i++) { max = i + toReplaceString.length() > sourceString.length() ? sourceString } }
調(diào)用webservice,可以首先根據(jù)wsdl文件生成客戶端,或者直接根據(jù)地址調(diào)用,下面討論直接調(diào)用地址的兩種不同方式:axis和Soap,soap方式主要是用在websphere下
axis方式調(diào)用:import java.util.Date; import java.text.DateFormat; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import javax.xml.namespace.QName; import java.lang.Integer; import javax.xml.rpc.ParameterMode; public class caClient {
public static void main(String[] args) { try { String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName("addUser"); call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE, javax.xml.rpc.ParameterMode.IN); call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); call.setUseSOAPAction(true); call.setSOAPActionURI("http://www.my.com/Rpc"); //Integer k = (Integer) call.invoke(new Object[] { i, j }); //System.out.println("result is " + k.toString() + "."); String temp = "測(cè)試人員"; String result = (String)call.invoke(new Object[]{temp}); System.out.println("result is "+result); } catch (Exception e) { System.err.println(e.toString()); } } } soap方式調(diào)用調(diào)用java生成的webserviceimport org.apache.soap.util.xml.*; import org.apache.soap.*; import org.apache.soap.rpc.*; import java.io.*; import java.net.*; import java.util.Vector; public class caService{ public static String getService(String user) { URL url = null; try { url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized"); } catch (MalformedURLException mue) { return mue.getMessage(); } // This is the main SOAP object Call soapCall = new Call(); // Use SOAP encoding soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); // This is the remote object we're asking for the price soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized"); // This is the name of the method on the above object soapCall.setMethodName("getUser"); // We need to send the ISBN number as an input parameter to the method Vector soapParams = new Vector(); // name, type, value, encoding style Parameter isbnParam = new Parameter("userName", String.class, user, null); soapParams.addElement(isbnParam); soapCall.setParams(soapParams); try { // Invoke the remote method on the object Response soapResponse = soapCall.invoke(url,""); // Check to see if there is an error, return "N/A" if (soapResponse.generatedFault()) { Fault fault = soapResponse.getFault(); String f = fault.getFaultString(); return f; } else { // read result Parameter soapResult = soapResponse.getReturnValue (); // get a string from the result return soapResult.getValue().toString(); } } catch (SOAPException se) { return se.getMessage(); } } } 返回一維數(shù)組時(shí) Parameter soapResult = soapResponse.getReturnValue(); String[] temp = (String[])soapResult.getValue(); 調(diào)用ASP.Net生成的webserviceprivate String HelloWorld(String uri, String u) { try { SOAPMappingRegistry smr = new SOAPMappingRegistry(); StringDeserializer sd = new StringDeserializer(); ArraySerializer arraySer = new ArraySerializer(); BeanSerializer beanSer = new BeanSerializer(); smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName( "http://tempuri.org/", "HelloWorldResult"), String.class, null, sd); smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName( "http://tempuri.org/", "temp"), String.class, beanSer, beanSer); URL url = new URL(uri); Call call = new Call(); call.setSOAPMappingRegistry(smr); call.setTargetObjectURI("urn:xmethods-Service1"); call.setMethodName("HelloWorld"); call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); Vector soapParams = new Vector(); soapParams.addElement(new Parameter("temp", String.class, u, null)); call.setParams(soapParams); Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld"); if (soapResponse.generatedFault()) { Fault fault = soapResponse.getFault(); System.out.println(fault); } else { Parameter soapResult = soapResponse.getReturnValue(); Object obj = soapResult.getValue(); System.out.println("===" + obj); } } catch (Exception e) { e.printStackTrace(); } return null; }Webservice開(kāi)發(fā)1. 發(fā)布環(huán)境:win2000 Professional + JDK 2. 下載Axis,解壓縮,將其webapps目錄下的axis拷貝到tomcat的webapps目錄下,進(jìn)行訪問(wèn)測(cè)試,http://localhost:8080/axis/ 出現(xiàn)正常頁(yè)面即可。 3. 下載包含wtp的Eclipse,解壓縮 4. 新建動(dòng)態(tài)Web Project,比如ca3,將axis下的jar包導(dǎo)入該項(xiàng)目的編譯環(huán)境里,在JavaSource中寫(xiě)java程序比如caSynrochnized,寫(xiě)好后,在上面點(diǎn)右鍵,選擇Create Web Service按照默認(rèn)設(shè)置,即可生成Web Service 在tomcat下部署5. 生成后,將eclipse下ca3\.deployables下的ca3目錄拷貝到tomcat的webapps目錄下 6. 設(shè)置axis的環(huán)境變量,如下 a) AXIS_HOME E:\Tomcat5.0\webapps\axis b) AXIS_LIB %AXIS_HOME%\WEB-INF\lib c) Classpath .;%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging- 7. 在E:\Tomcat 5.0\webapps\ca3\WEB-INF\caSynrochnizedService\com\hshz\ca找到deploy.wsdd文件,在dos命令行狀態(tài)下進(jìn)入上面目錄,運(yùn)行以下命令進(jìn)行發(fā)布 java org.apache.axis.client.AdminClient deploy.wsdd 8. 在IE中輸入http://localhost:8080/ca3/services可以看到已發(fā)布的webservice 在E:\Tomcat 5.0\webapps\ca3\wsdl目錄下找到wsdl文件,最后幾行比如<wsdlsoap:address location="http://localhost:8080/ca3/services/caSynrochnized"/>其中的location才是web Service相互調(diào)用的地址,另外localhost改為自己的IP地址。Webservice的重新部署
對(duì)于已發(fā)布的服務(wù),修改接口后,直接將發(fā)布目錄下的wsdl,以及wsdd,classes,service文件夾拷貝到tomcat相應(yīng)目錄下,不用重新發(fā)布即可。可先在瀏覽器中輸入地址/services進(jìn)行查看。 部署時(shí)可能遇到的問(wèn)題
1) 在dos窗口下執(zhí)行java org.apache.axis.client.AdminClient deploy.wsdd命令時(shí),出現(xiàn)404錯(cuò)誤,此時(shí)可能你的tomcat服務(wù)器沒(méi)有啟動(dòng),請(qǐng)先啟動(dòng)tomcat服務(wù)器。 2) 不同系統(tǒng)安裝相同的jdk版本,發(fā)布webservice服務(wù)時(shí),可能會(huì)出現(xiàn)unsupportedVersionException,如果在IE下敲入http://localhost:8080/java-oa/services,發(fā)現(xiàn)服務(wù)已經(jīng)發(fā)布成功,并且點(diǎn)wsdl鏈接能夠顯示wsdl文件,則此錯(cuò)誤可以忽略 3) 如果發(fā)現(xiàn)在啟動(dòng)tomcat時(shí),出現(xiàn)server-config.wsdd文件需要type,handle一類的錯(cuò)誤,則有可能你的應(yīng)用下存在gnujaxp.jar,因?yàn)檫@個(gè)jar包會(huì)與axis所需要的jar包相沖突,將gnujaxp.jar拷貝到common\lib下即可。 4) 如果webservice中的方法名字或者參數(shù)名或者參數(shù)數(shù)目,更改后需要重新發(fā)布webservice |