Java經過axis調用WebService

上午頭給了我一個任務,讓我對接別的公司的webservice接口,各類百度,看的頭暈腦花的,終於通了,記錄一下吧。java

jar包奉上,http://pan.baidu.com/s/1jSchCnode

包含:axis.jar,commons-discovery.jar,commons-logging-1.0.4.jar,jaxrpc.jar,wsdl4j.jar,mail.jar。web

爲何會有mail.jar呢?俺也不太清楚,若是沒有的話會報警告:apache

2014-10-22 15:07:45 org.apache.axis.utils.JavaUtils isAttachmentSupported
警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.

網上有不少說這個問題的,可是仍是沒鬧明白,仍是加上mail.jar穩妥點。json

對接webservice必定要有接口地址了dom

例如:學習

http://10.10.10.10:8080/xxxxx/services/XXXXWebservice?wsdl

直接上我寫的測試代碼吧(包名、地址、類名我都換成xxx了)測試

soapaction是經過訪問接口地址得到的,targetNamespaceui

<wsdl:definitions targetNamespace="http://webservice.xxxx.com">
package xx.xxxx.xxx;
 
import java.net.URL;
 
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
 
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
 
public class XXXWebService {
    public static void main(String[] args) {
        String soapaction = "http://webservice.xxxxxx.com";
        //你的webservice地址
        String endpoint = "http://10.10.10.10:8080/xxxxx/services/XXXXWebservice";
        Service service = new Service();
        try {
            Call call = (Call) service.createCall();
            call.setTimeout(new Integer(60000));
            call.setTargetEndpointAddress(new URL(endpoint));
            //你須要遠程調用的方法
            call.setOperationName(new QName(soapaction,"getXXXX"));
            //方法參數,若是沒有參數請無視
            call.addParameter(new QName(soapaction,"xxxxxx"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(soapaction,"xxxx"), XMLType.XSD_STRING, ParameterMode.IN);
            //設置返回類型,對方接口返回的json,我就用string接收了,自定義類型另貼一個代碼
            call.setReturnType(XMLType.XSD_STRING);
            //調用方法並傳遞參數,沒有參數的話: call.invoke(new Object[] { null});
            String result = (String) call.invoke(new Object[]{"xxxxx","xx,xx"});
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

下面是處理自定義類型        處理自定義返回類型我用的是dom4j  jar包: http://pan.baidu.com/s/1jGGfNZgspa

       call.setReturnType(XMLType.XSD_SCHEMA); 
            //沒有參數就寫個null
            Schema data = (Schema) call.invoke(new Object[] { null});
            MessageElement[] datas = data.get_any();
            for (int i = 0; i < datas.length; i++) {
                SAXReader reader = new org.dom4j.io.SAXReader();
                Document doc = reader.read(new ByteArrayInputStream(datas[i].toString().getBytes()));
                /* 例:這是你調用方法返回的文檔
                 * <ns:getXXXXXXX>
                 *     <ns:return type="xx.xxx.xxx.xxxx.XXXXX">
                 *         <ax21:objects type="xx.xxx.xxx.xxxx.XXXXX">
                 *             <ax21:attribute>巴拉巴拉</ax21:attribute>
                 *         </ax21:objects>
                 *     </ns:return>
                 * </ns:getXXXXXXX>
                 */
                //這裏的ns1:xxx 對應上面的ax21:objects
                Node node = doc.selectSingleNode("ns1:xxx");
                if (node != null) {
                    //這裏就是獲取object下的屬性了
                    Node attribute= node.selectSingleNode("ns1:attribute");
                    System.out.println(attribute);
                }
            }

以上就是經過axis調用webservice的所有內容了,如今只是會點皮毛,工做之餘仍是要多加學習:)

相關文章
相關標籤/搜索