如何从我的客户端使用Web服务?

时间:2013-03-05 17:18:37

标签: java web-services apache service wsdl

我创建了5种不同类型的服务。 A,B,C,d,E。使用Apache Axis

从单个Java客户端,我将调用所有这5个服务,并为每个服务提供3个args。

我创建了客户端。喜欢这样吧?

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

1 个答案:

答案 0 :(得分:0)

首先,使用IDE为每个Web服务WSDL创建subs。拥有存根之后,只需调用它们的虚拟方法即可。这为您节省了大量的时间和精力。

然后使用下面的代码逻辑,根据你的代码,你将无法调用所有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);
         }
    }