`

webservice之xfire的客户端调用

 
阅读更多

现在我们来看xfire的客户端调用,有两种方式:

一、通过服务端提供的接口类进行调用。

Java代码 复制代码
  1. package com.wujianjun.xfire.client;   
  2.   
  3. import java.net.MalformedURLException;   
  4. import java.util.List;   
  5.   
  6. import org.codehaus.xfire.XFire;   
  7. import org.codehaus.xfire.XFireFactory;   
  8. import org.codehaus.xfire.client.XFireProxyFactory;   
  9. import org.codehaus.xfire.service.Service;   
  10. import org.codehaus.xfire.service.binding.ObjectServiceFactory;   
  11.   
  12. import com.wujianjun.xfire.domain.Person;   
  13. import com.wujianjun.xfire.spring.IPersonService;   
  14.   
  15. public class PojoInvokeClient {   
  16.   
  17.     public static void main(String[] args) {   
  18.         Service serviceModel = new ObjectServiceFactory().create(IPersonService.class);   
  19.   
  20.         XFire xfire = XFireFactory.newInstance().getXFire();   
  21.         XFireProxyFactory factory = new XFireProxyFactory(xfire);   
  22.         String serviceUrl = "http://127.0.0.1:8080/xfire/services/PersonService";   
  23.   
  24.         IPersonService client = null;   
  25.         try {   
  26.             client = (IPersonService) factory.create(serviceModel, serviceUrl);   
  27.         } catch (MalformedURLException e) {   
  28.             System.out.println("Client call webservice has exception: "+ e.toString());   
  29.         }   
  30.   
  31.         String result1 =client.sayHello("张三");   
  32.            
  33.     }   

 二、直接通过url调用, 不用客户端提供接口类

Java代码 复制代码
  1. package com.wujianjun.xfire.client;   
  2.   
  3. import java.net.MalformedURLException;   
  4. import java.net.URL;   
  5.   
  6. import org.codehaus.xfire.client.Client;   
  7.   
  8. public class UrlInvokeClient {   
  9.   
  10.     public static void main(String[] args) {   
  11.         Client client = null;   
  12.         try {   
  13.             client = new Client(new URL("http://127.0.0.1:8080/xfire/PersonService.ws?wsdl"));   
  14.             Object[] result1 = client.invoke("sayHello", new Object[] {"张三"});   
  15.             System.out.println(result1[0]);   
  16.         } catch (MalformedURLException e) {   
  17.             e.printStackTrace();   
  18.         } catch (Exception e) {   
  19.             e.printStackTrace();   
  20.         }   
  21.     }   
  22. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics