現在我們來看xfire的客戶端調用,有兩種方式:
一、通過服務端提供的接口類進行調用。
package com.wujianjun.xfire.client;
import java.net.MalformedURLException;
import java.util.List;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.wujianjun.xfire.domain.Person;
import com.wujianjun.xfire.spring.IPersonService;
public class PojoInvokeClient {
public static void main(String[] args) {
Service serviceModel = new ObjectServiceFactory().create(IPersonService.class);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String serviceUrl = "http://127.0.0.1:8080/xfire/services/PersonService";
IPersonService client = null;
try {
client = (IPersonService) factory.create(serviceModel, serviceUrl);
} catch (MalformedURLException e) {
System.out.println("Client call webservice has exception: "+ e.toString());
}
String result1 =client.sayHello("張三");
}
}
二、直接通過url調用, 不用客戶端提供接口類
package com.wujianjun.xfire.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
public class UrlInvokeClient {
public static void main(String[] args) {
Client client = null;
try {
client = new Client(new URL("http://127.0.0.1:8080/xfire/PersonService.ws?wsdl"));
Object[] result1 = client.invoke("sayHello", new Object[] {"張三"});
System.out.println(result1[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}