Axis2調用WebService例子

      Axis2是Apache的一個開源組件,你可以從Apache Project的網站http://ws.apache.org/axis2/中下載它的zip包,解壓後,你會見到一個lib文件夾,在下例中,你需要導入以下幾個jar包

 

axiom-api.jar

axis2-kernel.jar

commons-logging.jar

axiom-impl.jar

wsdl4j.jar

XmlSchema.jar

backport-util-concurrent.jar

neethi.jar

commons-httpclient.jar

commons-codec.jar

 

下面這個是WebService的Impl文件,雖然寫客戶端不需要用到它,客戶端只需知道WebService的地址,並且由此找到最終發佈的wsdl文件,就可以調用這個WebService了。這個類採用了JSR-181(JAX-WS),只需在類中加入一些簡單的anotation,最終的WSDL文件及其它相關XML文件由這個類自動生成

package com.mycompany;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(name = "TestService", serviceName = "TestService", targetNamespace = "http://www.mycompany.com")
public class TestService
{
	@WebMethod(action = "http://www.mycompany.com/test")
	@WebResult(name = "greeting")
	public String test(@WebParam(name = "name") String name)
	{
		return "hello " + name;
	}
}

 

 

下面這個類,就是客戶端調用WebService的代碼

package javaapplication1;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class TestClient
{
    private static void invokeWebService()
    {
        try
        {
            String soapBindingAddress = "http://localhost:9080/WebService/TestService";
            
            EndpointReference endpointReference = new EndpointReference(soapBindingAddress);
            
            //創建一個OMFactory,下面的namespace、方法與參數均需由它創建
            OMFactory factory = OMAbstractFactory.getOMFactory();
            
            //下面創建命名空間,如果你的WebService指定了targetNamespace屬性的話,就要用這個
            //對應於@WebService(targetNamespace = "http://www.mycompany.com")
            OMNamespace namespace = factory.createOMNamespace("http://www.mycompany.com", "xsd");

            //下面創建的是參數對數,對應於@WebParam(name = "name")
            //由於@WebParam沒有指定targetNamespace,所以下面創建name參數時,用了null,否則你得賦一個namespace給它
            OMElement nameElement = nameElement = factory.createOMElement("name", null);
            nameElement.addChild(factory.createOMText(nameElement, "java"));

            //下面創建一個method對象,"test"爲方法名
            OMElement method = factory.createOMElement("test", namespace);
            method.addChild(nameElement);
            
            Options options = new Options();
            options.setAction("http://www.mycompany.com/test");  //此處對應於@WebMethod(action = "http://www.mycompany.com/test")
            options.setTo(endpointReference);

            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);

            //下面的輸出結果爲<xsd:test xmlns:xsd="http://www.mycompany.com"><name>java</name></xsd:test>
            System.out.println(method.toString());

            //發送並得到結果,至此,調用成功,並得到了結果
            OMElement result = sender.sendReceive(method);

            //下面的輸出結果爲<ns2:testResponse xmlns:ns2="http://www.mycompany.com"><greeting>hello java</greeting></ns2:testResponse>
            System.out.println(result.toString());
        }
        catch (AxisFault ex)
        {
            ex.printStackTrace();
        }

    }

    public static void main(String[] args)
    {
        TestClient.invokeWebService();
    }
} 

 

發佈了38 篇原創文章 · 獲贊 1 · 訪問量 5869
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章