How to access a web service using a regular Java class?

**My Web service class** import javax.jws.WebMethod; import javax.jws.WebService; /** * @author edward * */ @WebService public class HelloWeb { @WebMethod public String sayGreeting(String name) { return "Greeting " + name + "....!"; } } 

Java Server Class

 import javax.xml.ws.Endpoint; public class Server { public static void main(String[] args) { Endpoint.publish("http://localhost:9090/HelloWeb", new HelloWeb()); System.out.println("Hello Web service is ready"); } } 

The server is working correctly and I can access the service using the URL that returns the WSDL code. But I want to access the server using a unique URL in java.I have the following client java code:

Client for accessing the HelloWeb service

 import java.net.URL; import javax.xml.namespace.QName; import javax.xml.rpc.Service; import javax.xml.rpc.ServiceFactory; public class WebClient { String wsdl = "http://172.21.1.65:9090/HelloWeb?wsdl"; String namespace = "http://helloweb.com"; String serviceName = "HelloWebService"; QName serviceQN = new QName(namespace, serviceName); { try{ ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service service = serviceFactory.createService(new URL(wsdl), serviceQN); }catch (Exception e) { } } } 
+7
source share
1 answer

try this, note that I compiled and started your server in the "test" package, this is important. This is just a basic example to run JAX-WS.

 package test; import java.net.URL; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class WebClient { @WebService(name = "HelloWeb", targetNamespace = "http://test/") public interface HelloWeb { @WebMethod String sayGreeting(String name); } public static void main(String[] args) throws Exception { Service serv = Service.create(new URL( "http://localhost:9090/HelloWeb?wsdl"), new QName("http://test/", "HelloWebService")); HelloWeb p = serv.getPort(HelloWeb.class); System.out.println(p.sayGreeting("John")); } } 
+1
source

All Articles