2011-07-06 199 views
3

我使用JAX-WS制作了Web服务。现在我想使用Web浏览器进行测试,但出现错误。有人能解释我请帮助。Web服务测试

我的服务类:

package another; 
import javax.jws.WebService; 
import javax.xml.ws.Endpoint; 
@WebService(name = "WebService") 
public class WebServiceTest { 
    public String sayHello(String name) { 
     return "Hello : " + name; 
    } 

    public static void main(String[] args) { 
     WebServiceTest server = new WebServiceTest(); 
     Endpoint endpoint = Endpoint.publish(
       "http://localhost:9191/webServiceTest", server); 
    } 
} 

我运行这个类作为简单的Java程序。

而且我可以在我的浏览器中看到WSDL,网址为http://localhost:9191/webServiceTest?wsdl

我试图用URL http://localhost:9191/webServiceTest?sayHello?name=MKGandhi来调用它,但我没有得到任何结果。

这里有什么问题?

回答

2

我不能告诉你为什么不能在浏览器中测试它。 但至少我可以告诉你如何从你的代码测试,导致你的web服务的工作原理:“?”

package another; 

import javax.jws.WebService; 

@WebService 
public interface IWebServiceTest { 
    String sayHello(String name); 
} 

package another; 

import java.net.URL; 
import javax.xml.namespace.QName; 
import javax.xml.ws.Service; 

public class Main { 
    public static void main(String[] args) throws Exception { 
     String url = "http://localhost:9191/webServiceTest?wsdl"; 
     String namespace = "http://another/"; 
     QName serviceQN = new QName(namespace, "WebServiceTestService"); 
     Service service = Service.create(new URL(url), serviceQN); 

     String portName = "WebServicePort"; 
     QName portQN = new QName(namespace, portName); 

     IWebServiceTest sample = service.getPort(portQN, IWebServiceTest.class); 
     String result = sample.sayHello("blabla"); 
     System.out.println(result); 
    } 
} 
0

在您的网址的 “http://本地主机:?9191/WebServiceTest的sayHello的名称= MKGandhi”
尝试通过您的IP地址改变本地主机
例如: “HTTP:// 198.251.234.45:?9191/WebServiceTest的sayHello的名称= MKGandhi”

1

您尝试使用url http://localhost:9191/webServiceTest?sayHello?name=MKGandhi

测试你的Web服务就试试这个网址http://localhost:9191/webServiceTest/sayHello?name=MKGandhi

它应该工作正常:)