2013-03-05 60 views
0

我创建了5种不同类型的服务。 A,B,C,d,E。使用Apache Axis如何从我的客户端使用Web服务?

从单个java客户端我将调用所有这5个服务,并给每个服务3个参数。

我创建了客户端。像这样是对的?

import javax.xml.namespace.QName; 
import org.apache.axis.client.Call; 
import org.apache.axis.client.Service; 

public class ServicesCaller 
{ 

    String A=""; 
    String B=""; 
    String C=""; 

    public void services(String start,String end,String comfort) 
    { 
     try 
     { 
      String endpoint1="http://localhost:8080/callser/services/A1"; 
      String endpoint2="http://localhost:8080/callser/services/A2"; 
      String endpoint3="http://localhost:8080/callser/services/A3"; 
      String endpoint4="http://localhost:8080/callser/services/A4"; 
      String endpoint5="http://localhost:8080/callser/services/A5"; 

      Service service=new Service(); 

      Call call=(Call)service.createCall(); 

      call.setTargetEndpointAddress(new java.net.URL(endpoint1)); 
      call.setTargetEndpointAddress(new java.net.URL(endpoint2)); 
      call.setTargetEndpointAddress(new java.net.URL(endpoint3)); 
      call.setTargetEndpointAddress(new java.net.URL(endpoint4)); 
      call.setTargetEndpointAddress(new java.net.URL(endpoint5)); 

      call.setOperationName(new QName("http://service.com","firstReturn")); 

      String ret = (String) call.invoke(new Object[] {start,end,comfort}); 

     } 

     catch(Exception e) 
     { 
      System.out.println(e); 
     } 
    } 
} 

它正确吗?当我从我的jsp运行时,我得到这个异常

org.xml.sax.SAXException: Deserializing parameter 'arg0': could not find deserializer for type {http://schemas.xmlsoap.org/soap/encoding/}string 
+0

我建议您使用wsimport为您的WSDL生成一个Java客户端 – ftr 2013-03-05 17:27:07

回答

0

首先,使用IDE为每个Web服务WSDL创建子目录。有了存根后,只需拨打他们的虚拟方法即可。这为您节省了大量时间和精力。

然后使用下面的代码逻辑,根据您的代码,您将无法调用所有WS,但只能调用最后一个。

如果您没有IDE,那么下载Net Beans或Oracle Jdev,这两者都是免费软件,并且不需要许可证 如果您无法做到这一点,那么WSImport是您拥有的最佳选择。

public class ServicesCaller 
{ 

    String A=""; 
    String B=""; 
    String C=""; 

    public void services(String start,String end,String comfort) 
    { 
     try 
     { 
      String endpoint1="http://localhost:8080/callser/services/A1"; 
      String endpoint2="http://localhost:8080/callser/services/A2"; 
      String endpoint3="http://localhost:8080/callser/services/A3"; 
      String endpoint4="http://localhost:8080/callser/services/A4"; 
      String endpoint5="http://localhost:8080/callser/services/A5"; 

      Service service=new Service(); 

      Call call=(Call)service.createCall(); 

      String ret =""; 
      call.setOperationName(new QName("http://service.com","allepyReturn")); 

      call.setTargetEndpointAddress(new java.net.URL(endpoint1)); 
      ret = (String) call.invoke(new Object[] {start,end,comfort}); 

      call.setTargetEndpointAddress(new java.net.URL(endpoint2)); 
      ret = (String) call.invoke(new Object[] {start,end,comfort}); 

      call.setTargetEndpointAddress(new java.net.URL(endpoint3)); 
      ret = (String) call.invoke(new Object[] {start,end,comfort}); 

      call.setTargetEndpointAddress(new java.net.URL(endpoint4)); 
      ret = (String) call.invoke(new Object[] {start,end,comfort}); 

      call.setTargetEndpointAddress(new java.net.URL(endpoint5)); 
      ret = (String) call.invoke(new Object[] {start,end,comfort}); 

     } 

     catch(Exception e) 
     { 
      System.out.println(e); 
     } 
    } 
+0

如何为每个客户端创建存根(stub)?以及如何打电话给每个客户端 – 2013-03-06 05:06:51

+0

可以说你有Jdev,打开你的项目......将WSDL包含在项目中。然后右键单击WSDL并单击生成Web服务代理......一旦你完成它会带你通过一个向导,当你完成时,你得到所有的存根类和文件生成.. 所有的WSDL重复这一点。 – 2013-03-06 05:29:46

+0

我正在使用eclipse juno – 2013-03-06 05:38:30