1. 先說一下什麼是webservice
webservice的 發布一般都是使用WSDL(web service descriptive language)檔案的樣式來發布的,在WSDL檔案裡面,包含這個webservice暴露在外面可供使用的介面。
(1)WebService是一個SOA(面向服務的編程)的架構,它是不依賴於語言,不依賴於平台,可以實現不同的語言間的相互調用,通過Internet進行基於Http協議的網路應用間的互動。
(2)WebService實現不同語言間的調用,是依託於一個標準,webservice是需要遵守WSDL(web服務定義語言)/SOAP(簡單請求協議)規範的。
2. 下面是介紹如何使用axis調用webservice
(1)先從官網下載axis相關jar包,地址:http://axis.apache.org/axis/
官方要求引入如下jar包(將lib下的jar引入即可)
axis-<version>/lib/axis.jar axis-<version>/lib/jaxrpc.jar axis-<version>/lib/saaj.jar axis-<version>/lib/commons-logging-<version>.jar axis-<version>/lib/commons-discovery-<version>.jar axis-<version>/lib/wsdl4j-<version>.jar 我在項目中實際用到的最少jar如下: H:\libs\axis.jar
H:\libs\jaxrpc.jar
H:\libs\commons-logging-1.0.4.jar
H:\libs\commons-discovery-0.2.jar
H:\libs\wsdl4j-1.5.1.jar
(2)具體代碼實現
package ic;import javax.xml.rpc.ParameterMode;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis.encoding.XMLType;public class ICServiceTest1 {public static void main(String[] args) {try {String endpoint = "http://localhost:3000/hy/gw/queryConsignOrder?wsdl";Service service = new Service();Call call = (Call) service.createCall();call.setTargetEndpointAddress(endpoint);// WSDL裡面描述的介面名稱(要調用的方法)call.setOperationName("getLisQueryConsignOrder");// 介面方法的參數名, 參數類型,參數模式 IN(輸入), OUT(輸出) or INOUT(輸入輸出)call.addParameter("icSystem", XMLType.XSD_STRING, ParameterMode.IN);call.addParameter("companyFlag", XMLType.XSD_STRING, ParameterMode.IN);call.addParameter("consignOrderNo", XMLType.XSD_STRING, ParameterMode.IN);call.addParameter("orderNo", XMLType.XSD_STRING, ParameterMode.IN);// 設定被呼叫者法的傳回值類型call.setReturnType(XMLType.XSD_STRING);//設定方法中參數的值Object[] paramValues = new Object[] {"4PL","NJHY,NJHF","TC201309172206",""};// 給方法傳遞參數,並且調用方法String result = (String) call.invoke(paramValues);System.out.println("result is " + result);} catch (Exception e) {e.printStackTrace();}}}
===========================================異常解決=============================================================
今天在調用webserivce介面的時候出現了如下異常
警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.AxisFault faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server faultSubcode: faultString: org/apache/axis/client/Service faultActor: faultNode: faultDetail: {http://xml.apache.org/axis/}stackTrace:org/apache/axis/client/Serviceat org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)at org.apache.axis.client.Call.invokeEngine(Call.java:2784)at org.apache.axis.client.Call.invoke(Call.java:2767)at org.apache.axis.client.Call.invoke(Call.java:2443)at org.apache.axis.client.Call.invoke(Call.java:2366)at org.apache.axis.client.Call.invoke(Call.java:1812)at com.pcitc.lis.ws.lisforcrm.LisForCrmWebServicePortBindingStub.createPickupOrder(LisForCrmWebServicePortBindingStub.java:345)at com.pcitc.lis.ws.lisforcrm.LisForCrmWebServiceDelegateProxy.createPickupOrder(LisForCrmWebServiceDelegateProxy.java:74)at com.pcitc.lis.ws.lisforcrm.test.Test.main(Test.java:24){http://xml.apache.org/axis/}hostname:USER-20150617TZorg/apache/axis/client/Serviceat org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)at org.apache.axis.client.Call.invokeEngine(Call.java:2784)at org.apache.axis.client.Call.invoke(Call.java:2767)at org.apache.axis.client.Call.invoke(Call.java:2443)at org.apache.axis.client.Call.invoke(Call.java:2366)at org.apache.axis.client.Call.invoke(Call.java:1812)at com.pcitc.lis.ws.lisforcrm.LisForCrmWebServicePortBindingStub.createPickupOrder(LisForCrmWebServicePortBindingStub.java:345)at com.pcitc.lis.ws.lisforcrm.LisForCrmWebServiceDelegateProxy.createPickupOrder(LisForCrmWebServiceDelegateProxy.java:74)at com.pcitc.lis.ws.lisforcrm.test.Test.main(Test.java:24)
這種異常不是程式拋出來的,是由伺服器拋出的,所以在程式裡抓不到異常。
出現此種問題要注意一下是否已經把介面中用到的jar包加進了伺服器中或者項目中。
我用的webservice介面的伺服器為TOMCAT,此時需要將webservice介面中用到的jar到全部放到tomcat的lib下,這樣做後,異常解決