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);
- }
- }
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, opAddEntryArgs,classes)[0]);
- }