axis2-1.4.1 下的
axis2.war 放到tomcat下
簡單編寫一個服務,供給系統外部調用
Java代碼

- 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);
}
}

- 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");
- }