使用XFire調用WebService接口的步驟如下:
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-core</artifactId>
<version>1.2.6</version>
</dependency>
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;
Service serviceModel = new ObjectServiceFactory().create(serviceClass, serviceName, namespace, endpoint);
XFire xfire = XFireFactory.newInstance().getXFire();
serviceModel.setProperty(CommonsHttpMessageSender.HTTP_CLIENT, new HttpClient());
Client client = new Client(serviceModel, xfire.getTransportManager(), xfire.getBindingProvider());
client.setUrl("http://localhost:8080/mywebservice");
Object[] result = client.invoke(operationName, params);
完整的示例代碼如下:
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;
import org.apache.commons.httpclient.HttpClient;
public class XFireWebServiceClient {
public static void main(String[] args) throws Exception {
// 創建WebService的Service對象
Service service = new ObjectServiceFactory().create(ServiceInterface.class, "service", "http://localhost:8080/Service", null);
// 創建XFire對象
XFire xfire = XFireFactory.newInstance().getXFire();
// 創建WebService的Client對象
Client client = new Client(service, xfire.getTransportManager(), xfire.getBindingProvider());
// 設置WebService的地址
client.setUrl("http://localhost:8080/mywebservice");
// 調用WebService的方法
Object[] result = client.invoke("operationName", new Object[]{"param1", "param2"});
}
}
注意:在調用WebService接口之前,需要確保WebService的地址、命名空間、接口名和方法名等參數正確。