Javatpoint Logo

91-9990449935

 0120-4256464

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

  1. package com.javatpoint;  
  2. import javax.jws.WebMethod;  
  3. import javax.jws.WebService;  
  4. import javax.jws.soap.SOAPBinding;  
  5. import javax.jws.soap.SOAPBinding.Style;  
  6. //Service Endpoint Interface  
  7. @WebService  
  8. @SOAPBinding(style = Style.RPC)  
  9. public interface HelloWorld{  
  10.     @WebMethod String getHelloWorldAsString(String name);  
  11. }  

File: HelloWorldImpl.java

  1. package com.javatpoint;  
  2. import javax.jws.WebService;  
  3. //Service Implementation  
  4. @WebService(endpointInterface = "com.javatpoint.HelloWorld")  
  5. public class HelloWorldImpl implements HelloWorld{  
  6.     @Override  
  7.     public String getHelloWorldAsString(String name) {  
  8.         return "Hello World JAX-WS " + name;  
  9.     }  
  10. }  

File: Publisher.java

  1. package com.javatpoint;  
  2. import javax.xml.ws.Endpoint;  
  3. //Endpoint publisher  
  4. public class HelloWorldPublisher{  
  5.     public static void main(String[] args) {  
  6.        Endpoint.publish("http://localhost:7779/ws/hello"new HelloWorldImpl());  
  7.         }  
  8. }  

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

  1. package com.javatpoint;  
  2. import java.net.URL;  
  3. import javax.xml.namespace.QName;  
  4. import javax.xml.ws.Service;  
  5. public class HelloWorldClient{  
  6.     public static void main(String[] args) throws Exception {  
  7.     URL url = new URL("http://localhost:7779/ws/hello?wsdl");  
  8.    
  9.         //1st argument service URI, refer to wsdl document above  
  10.     //2nd argument is service name, refer to wsdl document above  
  11.         QName qname = new QName("http://javatpoint.com/""HelloWorldImplService");  
  12.         Service service = Service.create(url, qname);  
  13.         HelloWorld hello = service.getPort(HelloWorld.class);  
  14.         System.out.println(hello.getHelloWorldAsString("javatpoint rpc"));  
  15.      }  
  16.  }  

Output:

Hello World JAX-WS javatpoint rpc

Click me to download JAX-WS server example RPC style (eclipse)
Click me to download JAX-WS client example RPC style (eclipse)