เป็นตัวอย่างการ Call Web Service ของ Axis ครับซึ่งเหมาะสำหรับการ test โดยเฉพาะเพราะขั้นตอนการเขียน code ไม่มีอะไรมาก ตัวอย่าง code ดังนี้ครับ
public static void testWs(){
try {
URL url = new URL("http://localhost:7070/Axis/ExampleService");
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName("http://ws.fun4station.com", "sayHello"));
Object result = call.invoke(new Object[] {"Supot Saelao"});
System.out.println("++++RETURN OUTPUT PARAMETER++++");
Map<QName,Object> returnObject = call.getOutputParams();
for (Map.Entry<QName, Object> emap : returnObject.entrySet()){
System.out.println(" key: " + emap.getKey() + " value : " + emap.getValue());
}
System.out.println("++++RETURN OUTPUT VALUES++++");
List<Object> returnValue = call.getOutputValues();
for(Object obj : returnValue){
System.out.println(obj.toString());
}
System.out.println("++++RETURN SINGLE OBJECT++++");
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
สิ่งสำคัญก็ชื่อ บรรทัดนี้ครับ
call.setOperationName(new QName("http://ws.fun4station.com", "sayHello"));
QName(“p1″,”p2″)
โดยที่ P1 ก็คือ “targetNamespace” ใน wsdl ครับ ส่วน P2 ก็คือ Method ใน Web Service ที่สร้างมาครับ
และก็บรรทัดนี้ เป็นการส่ง Parameter ไปให้ Method ที่เราเรียกใช้ครับ โดยต้องส่งไปตามลำดับของ Parameter และ ชนิดของข้อมูลต้องถูกต้องครับ
Object result = call.invoke(new Object[] {"Supot Saelao"});
อ้างอิงจาก
BasicsGettingStarted