axis2 遠端呼叫webservice介面
阿新 • • 發佈:2019-02-19
1 import java.rmi.RemoteException; 2 import javax.xml.rpc.ParameterMode; 3 import javax.xml.rpc.ServiceException; 4 import org.apache.axis.client.Call; 5 import org.apache.axis.client.Service; 6 import org.apache.axis.encoding.XMLType; 7 8 public class HelloWorldTest { 9 10 public String invokeRemoteFuc() { 11 // 遠端呼叫路徑 12 String endpoint = "http://localhost:8080/WebServiceTest/services/HelloWorld"; 13 String result = "call failed!"; 14 Service service = new Service(); 15 Call call; 16 17 try { 18 call = (Call) service.createCall(); 19 call.setTargetEndpointAddress(endpoint); 20 // 呼叫的方法名 21 call.setOperationName("printStr"); 22 23 // 設定引數名 24 call.addParameter("name", // 引數名 25 XMLType.XSD_STRING, // 引數型別:String 26 ParameterMode.IN); // 引數模式:'IN' or 'OUT' 27 28 // 設定返回值型別 29 call.setReturnType(XMLType.XSD_STRING); // 返回值型別:String 30 String name = "Alexia"; 31 result = (String) call.invoke(new Object[] { name });// 遠端呼叫 32 } catch (ServiceException e) { 33 e.printStackTrace(); 34 } catch (RemoteException e) { 35 e.printStackTrace(); 36 } 37 38 return result; 39 } 40 41 // 測試 42 public static void main(String[] args) { 43 HelloWorldTest test = new HelloWorldTest(); 44 String result = test.invokeRemoteFuc(); 45 System.out.println(result); 46 } 47 48 }