如何从独立的Java客户端调用Web服务?

问题描述:

我有一个运行的回声Web服务可以说http://localhost:8080/axis2/services/Service1。该服务只是回显一个通过函数echo()发送给它的字符串。使用上述服务(Service.wsdl)的wsdl,我已经生成了(在eclipse中)ServiceStub.java和ServiceCallbackHandler.java。有了这两个文件,我该如何编写一个客户端来调用echo(String some_word)并接收回应?谢谢。如何从独立的Java客户端调用Web服务?

开始。如果你只是想测试/行使您的Web服务,我建议SOAPUI - http://www.soapui.org/

点它在你的WSDL,它将使你打电话给你Web服务方法。

事情是这样的:
(参见:Axis2 Web Service (Tomcat v6)

package com.gg.ws; 

import java.rmi.RemoteException; 

import com.gg.ws.ServiceStub.Echo; 
import com.gg.ws.ServiceStub.EchoResponse; 


public class WebServiceTest { 

    public void callEcho() throws RemoteException { 

     ServiceStub stub = new ServiceStub(); 

     Echo request = new Echo(); 
     request.setValue("Whatever"); 
     EchoResponse response = stub.echo(request); 
     System.out.println(" echo call response: " + response.get_return()); 
    } 
}