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方式调用 webservcieRPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();// 指定调用 WebService 的URTLEndpointReference 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, opAddEntryArgs,classes)[0]);}/** * axiom 方式调用服务端 * <功能详细描述> * @throws AxisFault * @throws Exception * @see [类、类#方法、类#成员] */public static void runAxiom() throws AxisFault {// 指定调用 WebService 的URTLEndpointReference 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
?