Search code examples
javaweb-servicesjax-ws

Web Service testing


I made web services using JAX-WS. Now I want to test using a web browser, but I am getting an error. Can somebody explain me please help.

My Service class:

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);
    }
}

I run this class as simple Java program.

And I can see the WSDL in my browser at http://localhost:9191/webServiceTest?wsdl.

And I am trying to call this using the URL http://localhost:9191/webServiceTest?sayHello?name=MKGandhi, but I am not getting any result.

What is wrong here?


Solution

  • I can't tell you why it is not possible to test it in browser. But at least I can tell you how to test it from your code, cause your webservice works:

    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);
        }
    }