JAX-WS Example Document Style

JAX-WS example document style for beginners and professionals with examples on soap, restful, rest, uddi, jax ws, jax rs, rpc, document, xml, java, jersey, resteasy, soa, jaxb, wsdl

JAX-WS Example Document Style

JAX-WS Example Document Style

Like RPC style, we can create JAX-WS examples in document style. To do so, you need to change only one line in the service interface.

You need to use Style.DOCUMENT for @SOAPBinding annotation in place of Style.RPC. Let's have a quick look at this:

File: HelloWorld.java

  1. @SOAPBinding(style = Style.DOCUMENT)//It is changed from RPC to DOCUMENT  

Now Let's see the full example of JAX-WS in document style.

There are created 4 files for hello world JAX-WS document style 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.hpnmaratt;  
  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.DOCUMENT)  
  9. public interface HelloWorld{  
  10.     @WebMethod String getHelloWorldAsString(String name);  

File: HelloWorldImpl.java

  1. package com.hpnmaratt;  
  2. import javax.jws.WebService;  
  3. //Service Implementation  
  4. @WebService(endpointInterface = "com.hpnmaratt.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.hpnmaratt;  
  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.         }  

If you run the publisher class, it may generate the following error:

Wrapper class com.javatpoint.GetHelloWorldAsString is not found. 
Have you run APT to generate them?

To solve the problem, go to the bin directory of your current project in command prompt and run the following command:

wsgen -keep -cp . com.javatpoint.HelloWorldImpl

Now, it will generator 2 files:

  • SayHello
  • SayHelloResponse

Paste these files in com.hpnmaratt directory and then run the publisher class.

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.hpnmaratt;  
  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");  
  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://hpnmaratt.com/""HelloWorldImplService");  
  12.         Service service = Service.create(url, qname);  
  13.         HelloWorld hello = service.getPort(HelloWorld.class);  
  14.         System.out.println(hello.getHelloWorldAsString("javatpoint document"));  
  15.      }  
  16.  }  


Hello World JAX-WS hpnmaratt document