微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

webservice 开发 axis2 简单部署服务

axis2-1.4.1 下的
axis2.war 放到tomcat下

简单编写一个服务,供给系统外部调用

import java.util.Random;



public class SimpleService {

	public String getGreeting(String name) {
		return "你好 "+name;
	}
	
	public int getPrice() {
		return new Random().nextInt(1000);
	}
}

 

SimpleService.class 放到 WEB-INF/pojo  下
就这么简单 就构成了一个服务

客户端(Java):

 

package client;

import java.rmi.remoteexception;
import java.util.Iterator;

import javax.xml.namespace.QName;

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.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class RPcclient {

	public static void main(String[] args) throws remoteexception {

		//RPC 方式调用 服务端
		//runRPC();
		
		//Axiom 方式调用 服务端
		//runAxiom();
		
		//wsdl2java.bat -uri http://localhost:8080/axis2/services/SimpleService?wsdl -p client -s -o stub
		//工具自动生成
		SimpleServiceStub stub = new SimpleServiceStub();
		SimpleServiceStub.GetGreeting gg = new SimpleServiceStub.GetGreeting();
		gg.setName("超人");
		System.out.println(stub.getGreeting(gg).get_return());
		System.out.println(stub.getPrice().get_return());
	}

	/**
	 * RPC方式调用 服务
	 * <功能详细描述>
	 * @throws AxisFault 
	 * @throws Exception
	 * @see [类、类#方法、类#成员]
	 */
	public static void runRPC() throws AxisFault {
		// 使用 RPC方式调用 webservcie
		RPCServiceClient serviceClient = new RPCServiceClient();
		Options options = serviceClient.getoptions();

		// 指定调用 WebService 的URTL
		EndpointReference taretEPR = new EndpointReference(
				"http://localhost:8080/axis2/services/SimpleService");
		options.setTo(taretEPR);
		
		//指定 getGreeting方法的参数值
		Object[] opAddEntryArgs =  new Object[]{"超人"};
		//指定 getGreeting方法的返回值的数据类型的class对象
		Class[] classes = new Class[]{String.class};
		//指定 要调用的 getGreeting方法及WSDL文件的命名空间
		QName opAddEntry = new QName("http://ws.apache.org/axis2","getGreeting");
		
		//调用getGreeting方法输出方法的返回值
		System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);
		
		classes = new Class[]{int.class};
		opAddEntry = new QName("http://ws.apache.org/axis2","getPrice");
		System.out.println(serviceClient.invokeBlocking(opAddEntry,classes)[0]);
	}
	
	/**
	 * axiom 方式调用服务端
	 * <功能详细描述>
	 * @throws AxisFault 
	 * @throws Exception 
	 * @see [类、类#方法、类#成员]
	 */
	public static void runAxiom() throws AxisFault {
		
		// 指定调用 WebService 的URTL
		EndpointReference taretEPR = new EndpointReference(
				"http://localhost:8080/axis2/services/SimpleService");
		
		//使用 axiom方式访问
		OMFactory fac = OMAbstractFactory.getoMFactory();
		OMNamespace omNs = fac.createOMNamespace("http://ws.apache.org/axis2","tns");
		OMElement method = fac.createOMElement("getGreeting",omNs);
		OMElement param1 = fac.createOMElement("name",omNs);
		param1.addChild(fac.createOMText(param1,"超人"));
		method.addChild(param1);
		
		Options options = new Options();
		options.setTo(taretEPR);
		options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
		
		ServiceClient axiomClient = new ServiceClient();
		axiomClient.setoptions(options);
		
		//开始访问
		axiomClient.fireAndForget(method);
		//1  
        OMElement result = axiomClient.sendReceive(method);  
        System.out.println("1:axiom客户端方式访问结果第一个element:"+result.getFirstElement().getText());  
          
        //2或者用下面遍历的方式获取访问结果  
        Iterator<OMElement> it=result.getChildElements();  
        while(it.hasNext()){  
            OMElement ome=(OMElement)it.next();  
            if(ome!=null){  
                String omevalue=ome.getText();  
                if (omevalue != null) {  
                    System.out.println("2:axiom客户端方式访问结果:"+omevalue);  
                }  
            }  
              
        }  
          
        //3使用QName参数  
        QName qname = new QName("http://device.axis2.hd.org","getGreeting");  
        result.getFirstChildWithName(qname);  
        System.out.println("3:axiom客户端方式QName访问结果第一个element:"+result.getFirstElement().getText());  
		
	}
}
 

结果:
[INFO] deploying module: MetadataExchange - file:/E:/workspacetest/axis2/Webroot/WEB-INF/lib/mex-1.4.1.jar
你好 超人
380

 

下一篇 使用  gsoap 生成客户端 C/C++ 实现客户端

webservice 开发 axis2 简单部署服务 gSoap 客户端通信

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐