Friday, 25 September 2015

JAX-WS Example RPC Style

JAX-WS Example RPC Style

Creating JAX-WS example is a easy task because it requires no extra configuration settings.
JAX-WS API is inbuilt in JDK, so you don't need to load any extra jar file for it. Let's see a simple example of JAX-WS example in RPC style.
There are created 4 files for hello world JAX-WS example:
  1. HelloWorld.java
  2. HelloWorldImpl.java
  3. Publisher.java
  4. HelloWorldClient.java
The first 3 files are created for server side and 1 application for client side.

JAX-WS Server Code

File: HelloWorld.java
package com.javatpoint;  
import javax.jws.WebMethod;  
import javax.jws.WebService;  
import javax.jws.soap.SOAPBinding;  
import javax.jws.soap.SOAPBinding.Style;  
//Service Endpoint Interface  
@WebService  
@SOAPBinding(style = Style.RPC)  
public interface HelloWorld{  
    @WebMethod String getHelloWorldAsString(String name);  
}  
File: HelloWorldImpl.java
package com.javatpoint;  
import javax.jws.WebService;  
//Service Implementation  
@WebService(endpointInterface = "com.javatpoint.HelloWorld")  
public class HelloWorldImpl implements HelloWorld{  
    @Override  
    public String getHelloWorldAsString(String name) {  
        return "Hello World JAX-WS " + name;  
    }
}
File: Publisher.java
package com.javatpoint;  
import javax.xml.ws.Endpoint;  
//Endpoint publisher  
public class HelloWorldPublisher{  
    public static void main(String[] args) {  
       Endpoint.publish("http://localhost:7779/ws/hello"new HelloWorldImpl());  
        }
}  

How to view generated WSDL

After running the publisher code, you can see the generated WSDL file by visiting the URL:
  1. http://localhost:7779/ws/hello?wsdl  

JAX-WS Client Code

File: HelloWorldClient.java
package com.javatpoint;  
import java.net.URL;  
import javax.xml.namespace.QName;  
import javax.xml.ws.Service;  
public class HelloWorldClient{  
    public static void main(String[] args) throws Exception {  
    URL url = new URL("http://localhost:7779/ws/hello?wsdl");  
 
        //1st argument service URI, refer to wsdl document above  
    //2nd argument is service name, refer to wsdl document above  
        QName qname = new QName("http://javatpoint.com/""HelloWorldImplService");  
        Service service = Service.create(url, qname);
        HelloWorld hello = service.getPort(HelloWorld.class);  
        System.out.println(hello.getHelloWorldAsString("javaforbegnnerstutorialrpc"));  
     }
 }  
Output:
Hello World JAX-WS javaforbegnnerstutorial rpc 

No comments:

Post a Comment