Understanding JAX-WS wsgen with example


What is wsgen?


wsgen is a command line tool provided by JAX-WS to generate all the artifacts required for web service deployment and invocation.

In simple words, wsgen takes web service implementation class as an input and generates all the required artifacts for web service deployment.

It’s provided as part of Java 1.6 and it is available under JDK_PATH/bin folder

When to use wsgen?


We should use it in the below 2 scenarios

1) Whenever we want to generate web service artifacts (java files) for web service deployment
This is mostly required for Document style web service development

2) Whenever we want to provide Web service consuming details to web service consumers,In this case its required to generate WSDL and XSD files.
As WSDL file is the most important file for the web service consumers to understand and invoke the web service.
Consumers will use this WSDL file to develop the client and consume the published web service.

wsgen command syntax

  1. wsgen -keep -cp . webServiceImplClass
wsgen -keep -cp . webServiceImplClass


-keep option specifies that it has to keep the generated files.
-cp specifies the classpath
. Indicates current directory
webServiceImplClass – it’s the web service implementation class

Requirement:

Create a web service to find whether given number is prime or not and develop a client which consumes it.

Let’s implement this requirement with below steps


Step 1

Create a new Java maven project in eclipse

jax-ws-wsgen-cmd-proj-structure

Step 2

Create a service implementation class

  1. package com.kb.ws;
  2.  
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebService;
  5.  
  6. @WebService
  7. public class PrimeNumberServiceImpl{
  8.  
  9.     @WebMethod
  10.     public boolean isPrimeNumber(int number) {
  11.         for(int i=2;i<=number/2;i++){
  12.             if(number % i == 0)
  13.                 return false;
  14.         }
  15.         return true;
  16.     }
  17.  
  18. }
package com.kb.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class PrimeNumberServiceImpl{

	@WebMethod
	public boolean isPrimeNumber(int number) {
		for(int i=2;i<=number/2;i++){
			if(number % i == 0)
				return false;
		}
		return true;
	}

}


Since we are directly adding @webMethod annotation to the methods in the implementation class, we don’t need to use interface for the same.

Step 3

Generate web service artifacts(java files)

We will use wsgen command to generate the required artifacts by using above implementation class (PrimeNumberServiceImpl.java)

Open command prompt, go to the below path

D:\workspace\WebServices\WsgenJaxws\target\classes

Issue the following command:

  1. wsgen -keep -cp . com.kb.ws.PrimeNumberServiceImpl
wsgen -keep -cp . com.kb.ws.PrimeNumberServiceImpl


jax-ws-wsgen-cmd-output

Note
The classpath specified via -cp should point to the directory that contains the compiled classes, typically the target/classes folder in maven project and bin folder in java project.

Now check the below files are generated in the same path from where we issued the command

jax-ws-wsgen-cmd-output-files

Generated source files are as below

IsPrimeNumber.java

  1. package com.kb.ws.client;
  2.  
  3. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. import javax.xml.bind.annotation.XmlElement;
  6. import javax.xml.bind.annotation.XmlRootElement;
  7. import javax.xml.bind.annotation.XmlType;
  8.  
  9. @XmlRootElement(name = "isPrimeNumber", namespace = "http://ws.kb.com/")
  10. @XmlAccessorType(XmlAccessType.FIELD)
  11. @XmlType(name = "isPrimeNumber", namespace = "http://ws.kb.com/")
  12. public class IsPrimeNumber {
  13.  
  14.     @XmlElement(name = "arg0", namespace = "")
  15.     private int arg0;
  16.  
  17.     /**
  18.      *
  19.      * @return
  20.      *     returns int
  21.      */
  22.     public int getArg0() {
  23.         return this.arg0;
  24.     }
  25.  
  26.     /**
  27.      *
  28.      * @param arg0
  29.      *     the value for the arg0 property
  30.      */
  31.     public void setArg0(int arg0) {
  32.         this.arg0 = arg0;
  33.     }
  34.  
  35. }
package com.kb.ws.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "isPrimeNumber", namespace = "http://ws.kb.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "isPrimeNumber", namespace = "http://ws.kb.com/")
public class IsPrimeNumber {

    @XmlElement(name = "arg0", namespace = "")
    private int arg0;

    /**
     * 
     * @return
     *     returns int
     */
    public int getArg0() {
        return this.arg0;
    }

    /**
     * 
     * @param arg0
     *     the value for the arg0 property
     */
    public void setArg0(int arg0) {
        this.arg0 = arg0;
    }

}


IsPrimeNumberResponse.java

  1. package com.kb.ws.client;
  2.  
  3. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. import javax.xml.bind.annotation.XmlElement;
  6. import javax.xml.bind.annotation.XmlRootElement;
  7. import javax.xml.bind.annotation.XmlType;
  8.  
  9. @XmlRootElement(name = "isPrimeNumberResponse", namespace = "http://ws.kb.com/")
  10. @XmlAccessorType(XmlAccessType.FIELD)
  11. @XmlType(name = "isPrimeNumberResponse", namespace = "http://ws.kb.com/")
  12. public class IsPrimeNumberResponse {
  13.  
  14.     @XmlElement(name = "return", namespace = "")
  15.     private boolean _return;
  16.  
  17.     /**
  18.      *
  19.      * @return
  20.      *     returns boolean
  21.      */
  22.     public boolean isReturn() {
  23.         return this._return;
  24.     }
  25.  
  26.     /**
  27.      *
  28.      * @param _return
  29.      *     the value for the _return property
  30.      */
  31.     public void setReturn(boolean _return) {
  32.         this._return = _return;
  33.     }
  34.  
  35. }
package com.kb.ws.client;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "isPrimeNumberResponse", namespace = "http://ws.kb.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "isPrimeNumberResponse", namespace = "http://ws.kb.com/")
public class IsPrimeNumberResponse {

    @XmlElement(name = "return", namespace = "")
    private boolean _return;

    /**
     * 
     * @return
     *     returns boolean
     */
    public boolean isReturn() {
        return this._return;
    }

    /**
     * 
     * @param _return
     *     the value for the _return property
     */
    public void setReturn(boolean _return) {
        this._return = _return;
    }

}


Step 4

Copy these files to our project and adjust the package names in the java files accordingly

Step 5

Now generate WSDL file using wsgen command

We just need to add –wsdl option to wsgen command to generate the WSDL and XSD files

Issue the below command from command window

  1. wsgen -keep -cp . com.kb.ws.PrimeNumberServiceImpl –wsdl
wsgen -keep -cp . com.kb.ws.PrimeNumberServiceImpl –wsdl


/jax-ws-wsgen-wsdl-generation-cmd-output


Step 6

Check the generated wsdl and xsd files

jax-ws-wsgen-wsdl-generation-output-files

Generated wsdl file

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
  3. <definitions targetNamespace="http://ws.kb.com/" name="PrimeNumberServiceImplService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:tns="http://ws.kb.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata">
  4.   <types>
  5.     <xsd:schema>
  6.       <xsd:import namespace="http://ws.kb.com/" schemaLocation="PrimeNumberServiceImplService_schema1.xsd"/>
  7.     </xsd:schema>
  8.   </types>
  9.   <message name="isPrimeNumber">
  10.     <part name="parameters" element="tns:isPrimeNumber"/>
  11.   </message>
  12.   <message name="isPrimeNumberResponse">
  13.     <part name="parameters" element="tns:isPrimeNumberResponse"/>
  14.   </message>
  15.   <portType name="PrimeNumberServiceImpl">
  16.     <operation name="isPrimeNumber">
  17.       <input wsam:Action="http://ws.kb.com/PrimeNumberServiceImpl/isPrimeNumberRequest" message="tns:isPrimeNumber"/>
  18.       <output wsam:Action="http://ws.kb.com/PrimeNumberServiceImpl/isPrimeNumberResponse" message="tns:isPrimeNumberResponse"/>
  19.     </operation>
  20.   </portType>
  21.   <binding name="PrimeNumberServiceImplPortBinding" type="tns:PrimeNumberServiceImpl">
  22.     <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
  23.     <operation name="isPrimeNumber">
  24.       <soap:operation soapAction=""/>
  25.       <input>
  26.         <soap:body use="literal"/>
  27.       </input>
  28.       <output>
  29.         <soap:body use="literal"/>
  30.       </output>
  31.     </operation>
  32.   </binding>
  33.   <service name="PrimeNumberServiceImplService">
  34.     <port name="PrimeNumberServiceImplPort" binding="tns:PrimeNumberServiceImplPortBinding">
  35.       <soap:address location="REPLACE_WITH_ACTUAL_URL"/>
  36.     </port>
  37.   </service>
  38. </definitions>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<definitions targetNamespace="http://ws.kb.com/" name="PrimeNumberServiceImplService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:tns="http://ws.kb.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://ws.kb.com/" schemaLocation="PrimeNumberServiceImplService_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="isPrimeNumber">
    <part name="parameters" element="tns:isPrimeNumber"/>
  </message>
  <message name="isPrimeNumberResponse">
    <part name="parameters" element="tns:isPrimeNumberResponse"/>
  </message>
  <portType name="PrimeNumberServiceImpl">
    <operation name="isPrimeNumber">
      <input wsam:Action="http://ws.kb.com/PrimeNumberServiceImpl/isPrimeNumberRequest" message="tns:isPrimeNumber"/>
      <output wsam:Action="http://ws.kb.com/PrimeNumberServiceImpl/isPrimeNumberResponse" message="tns:isPrimeNumberResponse"/>
    </operation>
  </portType>
  <binding name="PrimeNumberServiceImplPortBinding" type="tns:PrimeNumberServiceImpl">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="isPrimeNumber">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="PrimeNumberServiceImplService">
    <port name="PrimeNumberServiceImplPort" binding="tns:PrimeNumberServiceImplPortBinding">
      <soap:address location="REPLACE_WITH_ACTUAL_URL"/>
    </port>
  </service>
</definitions>


Generated xsd file

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <xs:schema version="1.0" targetNamespace="http://ws.kb.com/" xmlns:tns="http://ws.kb.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  3.  
  4.   <xs:element name="isPrimeNumber" type="tns:isPrimeNumber"/>
  5.  
  6.   <xs:element name="isPrimeNumberResponse" type="tns:isPrimeNumberResponse"/>
  7.  
  8.   <xs:complexType name="isPrimeNumber">
  9.     <xs:sequence>
  10.       <xs:element name="arg0" type="xs:int"/>
  11.     </xs:sequence>
  12.   </xs:complexType>
  13.  
  14.   <xs:complexType name="isPrimeNumberResponse">
  15.     <xs:sequence>
  16.       <xs:element name="return" type="xs:boolean"/>
  17.     </xs:sequence>
  18.   </xs:complexType>
  19. </xs:schema>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://ws.kb.com/" xmlns:tns="http://ws.kb.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="isPrimeNumber" type="tns:isPrimeNumber"/>

  <xs:element name="isPrimeNumberResponse" type="tns:isPrimeNumberResponse"/>

  <xs:complexType name="isPrimeNumber">
    <xs:sequence>
      <xs:element name="arg0" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="isPrimeNumberResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:boolean"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>



Step 7

Create the web service Publisher class

  1. package com.kb.endpoint;
  2.  
  3. import javax.xml.ws.Endpoint;
  4.  
  5. import com.kb.ws.PrimeNumberServiceImpl;
  6.  
  7. public class PrimeNumberPublisher {
  8.     public static void main(String[] args) {
  9.         Endpoint endpoint = Endpoint.create(new PrimeNumberServiceImpl());
  10.         endpoint.publish("http://localhost:8888/ws/primeNumber");
  11.  
  12.        
  13.         //Endpoint.publish("http://localhost:8888/ws/primeNumber", new PrimeNumberServiceImpl());
  14.     }
  15.  
  16. }
package com.kb.endpoint;

import javax.xml.ws.Endpoint;

import com.kb.ws.PrimeNumberServiceImpl;

public class PrimeNumberPublisher {
	public static void main(String[] args) {
		Endpoint endpoint = Endpoint.create(new PrimeNumberServiceImpl());
		endpoint.publish("http://localhost:8888/ws/primeNumber");

		
		//Endpoint.publish("http://localhost:8888/ws/primeNumber", new PrimeNumberServiceImpl());
	}

}


That's all, we are done,Run the above class to publish the web service and provide WSDL file to web service consumers

Download this project WsgenJaxws.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on