Understanding JAX-WS wsimport with example

What is wsimport ?

Wsimport is a command line tool provided by JAX-WS to generate all the web services artifacts.
Web service artifacts include web service client support code which will take care of including qualified name and url in the client supporting class.

Generated Web service artifacts (java classes) will be used by web service client to access the published web service.

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

When to use wsimport ?

We should use it whenever we get WSDL file from service provider

Web service provider provides a WSDL file after publishing a web service.

Web service provider provides the URL of the WSDL file as below
http://companyX.com/ws/sampleService?wsdl

In this situation any client or consumer can run wsimport command by passing wsdl file to generate the required client supported files which will be used to consume the published service

wsimport command syntax
  1. wsimport -keep -p client http: //companyX.com/ws/sampleService?wsdl

wsimport -keep -p client http://companyX.com/ws/sampleService?wsdl


-keep option specifies that it has to keep the generated files.
-p allows us to specify the java package name in which the generated files are to be placed.
We have given the package name as "client" so that it will keep the generated files in client package.

Let's take wsdl file generated in the previous article

Assume this wsdl file is provided by some company and we have to use it to invoke their published service

  1. <!--

  2.  Published 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. -->

  4. <!--

  5.  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.

  6. -->

  7. <definitions xmlns:wsu= "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"

  8. xmlns:wsp= "http://www.w3.org/ns/ws-policy"

  9. xmlns:wsp1_2= "http://schemas.xmlsoap.org/ws/2004/09/policy"

  10. xmlns:wsam= "http://www.w3.org/2007/05/addressing/metadata"

  11. xmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/"

  12. xmlns:tns= "http://ws.kb.com/" xmlns:xsd= "http://www.w3.org/2001/XMLSchema"

  13. xmlns= "http://schemas.xmlsoap.org/wsdl/" targetNamespace= "http://ws.kb.com/"name= "PrimeNumberServiceImplService" >

  14. <types/>

  15. <message name= "isPrimeNumber" >

  16. <part name= "arg0" type= "xsd:int" />

  17. </message>

  18. <message name= "isPrimeNumberResponse" >

  19. <part name= "return" type= "xsd:boolean" />

  20. </message>

  21. <portType name= "PrimeNumberService" >

  22. <operation name= "isPrimeNumber" >

  23. <input wsam: Action = "http://ws.kb.com/PrimeNumberService/isPrimeNumberRequest" message= "tns:isPrimeNumber" />

  24. <output wsam: Action = "http://ws.kb.com/PrimeNumberService/isPrimeNumberResponse"

  25.                                                                               message= "tns:isPrimeNumberResponse" />

  26. </operation>

  27. </portType>

  28. <binding name= "PrimeNumberServiceImplPortBinding" type= "tns:PrimeNumberService" >

  29. <soap:binding transport= "http://schemas.xmlsoap.org/soap/http" style= "rpc" />

  30. <operation name= "isPrimeNumber" >

  31. <soap:operation soapAction= "" />

  32. <input>

  33. <soap:body use= "literal" namespace= "http://ws.kb.com/" />

  34. </input>

  35. <output>

  36. <soap:body use= "literal" namespace= "http://ws.kb.com/" />

  37. </output>

  38. </operation>

  39. </binding>

  40. <service name= "PrimeNumberServiceImplService" >

  41. <port name= "PrimeNumberServiceImplPort" binding= "tns:PrimeNumberServiceImplPortBinding" >

  42. <soap:address location= "http://localhost:8888/ws/primeNumber" />

  43. </port>

  44. </service>

  45. </definitions>

<!--  Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.  --> <!--  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 xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"  xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.kb.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.kb.com/"name="PrimeNumberServiceImplService"> <types/>  <message name="isPrimeNumber">      <part name="arg0" type="xsd:int"/> </message>  <message name="isPrimeNumberResponse">      <part name="return" type="xsd:boolean"/> </message>  <portType name="PrimeNumberService">     <operation name="isPrimeNumber">       <input wsam:Action="http://ws.kb.com/PrimeNumberService/isPrimeNumberRequest" message="tns:isPrimeNumber"/>       <output wsam:Action="http://ws.kb.com/PrimeNumberService/isPrimeNumberResponse"                                                                                           message="tns:isPrimeNumberResponse"/>     </operation> </portType>  <binding name="PrimeNumberServiceImplPortBinding" type="tns:PrimeNumberService">      <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>      <operation name="isPrimeNumber">      <soap:operation soapAction=""/>      <input>          <soap:body use="literal" namespace="http://ws.kb.com/"/>      </input>      <output>          <soap:body use="literal" namespace="http://ws.kb.com/"/>      </output>      </operation> </binding>  <service name="PrimeNumberServiceImplService"> <port name="PrimeNumberServiceImplPort" binding="tns:PrimeNumberServiceImplPortBinding"> <soap:address location="http://localhost:8888/ws/primeNumber"/> </port> </service> </definitions>
Lets see the steps to be followed as a consumer to consume the published web service

Step 1

Run wsimport command by passing wsdl file as below

  1. wsimport -keep -p client http: //localhost:8888/ws/primeNumber?wsdl

wsimport -keep -p client http://localhost:8888/ws/primeNumber?wsdl


Output of wsimport command

jax-ws-prime-number-wsimport-cmd-output

Now client code is generated under the path from where we issued wsimport command

We can see 2 source files and 2 class files generated in my folder

jax-ws-prime-number-wsimport-cmd-output-files

Generated files are as below

PrimeNumberService.java

  1. package com.kb.ws.client ;

  2. import javax.jws.WebMethod ;

  3. import javax.jws.WebParam ;

  4. import javax.jws.WebResult ;

  5. import javax.jws.WebService ;

  6. import javax.jws.soap.SOAPBinding ;

  7. import javax.xml.ws.Action ;

  8. /**

  9.  * This class was generated by the JAX-WS RI.

  10.  * JAX-WS RI 2.2.9-b130926.1035

  11.  * Generated source version: 2.2

  12.  *

  13.  */

  14. @WebService(name = "PrimeNumberService", targetNamespace = "http://ws.kb.com/" )

  15. @SOAPBinding(style = SOAPBinding.Style.RPC )

  16. public interface PrimeNumberService {

  17. /**

  18.      *

  19.      * @param arg0

  20.      * @return

  21.      *     returns boolean

  22.      */

  23.     @WebMethod

  24.     @WebResult(partName = "return" )

  25.     @Action (input = "http://ws.kb.com/PrimeNumberService/isPrimeNumberRequest", output = "http://ws.kb.com/PrimeNumberService/isPrimeNumberResponse" )

  26. public boolean isPrimeNumber(

  27.         @WebParam(name = "arg0", partName = "arg0" )

  28. int arg0) ;

  29. }

package com.kb.ws.client;  import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.xml.ws.Action;   /**  * This class was generated by the JAX-WS RI.  * JAX-WS RI 2.2.9-b130926.1035  * Generated source version: 2.2  *   */ @WebService(name = "PrimeNumberService", targetNamespace = "http://ws.kb.com/") @SOAPBinding(style = SOAPBinding.Style.RPC) public interface PrimeNumberService {       /**      *       * @param arg0      * @return      *     returns boolean      */     @WebMethod     @WebResult(partName = "return")     @Action(input = "http://ws.kb.com/PrimeNumberService/isPrimeNumberRequest", output = "http://ws.kb.com/PrimeNumberService/isPrimeNumberResponse")     public boolean isPrimeNumber(         @WebParam(name = "arg0", partName = "arg0")         int arg0);  }


PrimeNumberServiceImplService.java

  1. package com.kb.ws.client ;

  2. import java.net.MalformedURLException ;

  3. import java.net.URL ;

  4. import javax.xml.namespace.QName ;

  5. import javax.xml.ws.Service ;

  6. import javax.xml.ws.WebEndpoint ;

  7. import javax.xml.ws.WebServiceClient ;

  8. import javax.xml.ws.WebServiceException ;

  9. import javax.xml.ws.WebServiceFeature ;

  10. /**

  11.  * This class was generated by the JAX-WS RI.

  12.  * JAX-WS RI 2.2.9-b130926.1035

  13.  * Generated source version: 2.2

  14.  *

  15.  */

  16. @WebServiceClient(name = "PrimeNumberServiceImplService", targetNamespace = "http://ws.kb.com/", wsdlLocation = "http://localhost:8888/ws/primeNumber?wsdl" )

  17. public class PrimeNumberServiceImplService

  18. extends Service

  19. {

  20. private final static URL PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION;

  21. private final static WebServiceException PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION;

  22. private final static QName PRIMENUMBERSERVICEIMPLSERVICE_QNAME = new QName( "http://ws.kb.com/", "PrimeNumberServiceImplService" ) ;

  23. static {

  24. URL url = null ;

  25.         WebServiceException e = null ;

  26. try {

  27.             url = new URL ( "http://localhost:8888/ws/primeNumber?wsdl" ) ;

  28. } catch ( MalformedURLException ex) {

  29.             e = new WebServiceException(ex) ;

  30. }

  31.         PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION = url;

  32.         PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION = e;

  33. }

  34. public PrimeNumberServiceImplService( ) {

  35. super (__getWsdlLocation( ), PRIMENUMBERSERVICEIMPLSERVICE_QNAME) ;

  36. }

  37. public PrimeNumberServiceImplService(WebServiceFeature... features ) {

  38. super (__getWsdlLocation( ), PRIMENUMBERSERVICEIMPLSERVICE_QNAME, features) ;

  39. }

  40. public PrimeNumberServiceImplService( URL wsdlLocation) {

  41. super (wsdlLocation, PRIMENUMBERSERVICEIMPLSERVICE_QNAME) ;

  42. }

  43. public PrimeNumberServiceImplService( URL wsdlLocation, WebServiceFeature... features ) {

  44. super (wsdlLocation, PRIMENUMBERSERVICEIMPLSERVICE_QNAME, features) ;

  45. }

  46. public PrimeNumberServiceImplService( URL wsdlLocation, QName serviceName) {

  47. super (wsdlLocation, serviceName) ;

  48. }

  49. public PrimeNumberServiceImplService( URL wsdlLocation, QName serviceName, WebServiceFeature... features ) {

  50. super (wsdlLocation, serviceName, features) ;

  51. }

  52. /**

  53.      *

  54.      * @return

  55.      *     returns PrimeNumberService

  56.      */

  57.     @WebEndpoint(name = "PrimeNumberServiceImplPort" )

  58. public PrimeNumberService getPrimeNumberServiceImplPort( ) {

  59. return super.getPort ( new QName( "http://ws.kb.com/", "PrimeNumberServiceImplPort" ), PrimeNumberService.class ) ;

  60. }

  61. /**

  62.      *

  63.      * @param features

  64.      *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.

  65.      * @return

  66.      *     returns PrimeNumberService

  67.      */

  68.     @WebEndpoint(name = "PrimeNumberServiceImplPort" )

  69. public PrimeNumberService getPrimeNumberServiceImplPort(WebServiceFeature... features ) {

  70. return super.getPort ( new QName( "http://ws.kb.com/", "PrimeNumberServiceImplPort" ), PrimeNumberService.class, features) ;

  71. }

  72. private static URL __getWsdlLocation( ) {

  73. if (PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION!= null ) {

  74. throw PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION;

  75. }

  76. return PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION;

  77. }

  78. }

package com.kb.ws.client;  import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceException; import javax.xml.ws.WebServiceFeature;   /**  * This class was generated by the JAX-WS RI.  * JAX-WS RI 2.2.9-b130926.1035  * Generated source version: 2.2  *   */ @WebServiceClient(name = "PrimeNumberServiceImplService", targetNamespace = "http://ws.kb.com/", wsdlLocation = "http://localhost:8888/ws/primeNumber?wsdl") public class PrimeNumberServiceImplService     extends Service {      private final static URL PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION;     private final static WebServiceException PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION;     private final static QName PRIMENUMBERSERVICEIMPLSERVICE_QNAME = new QName("http://ws.kb.com/", "PrimeNumberServiceImplService");      static {         URL url = null;         WebServiceException e = null;         try {             url = new URL("http://localhost:8888/ws/primeNumber?wsdl");         } catch (MalformedURLException ex) {             e = new WebServiceException(ex);         }         PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION = url;         PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION = e;     }      public PrimeNumberServiceImplService() {         super(__getWsdlLocation(), PRIMENUMBERSERVICEIMPLSERVICE_QNAME);     }      public PrimeNumberServiceImplService(WebServiceFeature... features) {         super(__getWsdlLocation(), PRIMENUMBERSERVICEIMPLSERVICE_QNAME, features);     }      public PrimeNumberServiceImplService(URL wsdlLocation) {         super(wsdlLocation, PRIMENUMBERSERVICEIMPLSERVICE_QNAME);     }      public PrimeNumberServiceImplService(URL wsdlLocation, WebServiceFeature... features) {         super(wsdlLocation, PRIMENUMBERSERVICEIMPLSERVICE_QNAME, features);     }      public PrimeNumberServiceImplService(URL wsdlLocation, QName serviceName) {         super(wsdlLocation, serviceName);     }      public PrimeNumberServiceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {         super(wsdlLocation, serviceName, features);     }      /**      *       * @return      *     returns PrimeNumberService      */     @WebEndpoint(name = "PrimeNumberServiceImplPort")     public PrimeNumberService getPrimeNumberServiceImplPort() {         return super.getPort(new QName("http://ws.kb.com/", "PrimeNumberServiceImplPort"), PrimeNumberService.class);     }      /**      *       * @param features      *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.      * @return      *     returns PrimeNumberService      */     @WebEndpoint(name = "PrimeNumberServiceImplPort")     public PrimeNumberService getPrimeNumberServiceImplPort(WebServiceFeature... features) {         return super.getPort(new QName("http://ws.kb.com/", "PrimeNumberServiceImplPort"), PrimeNumberService.class, features);     }      private static URL __getWsdlLocation() {         if (PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION!= null) {             throw PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION;         }         return PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION;     }  }


wsimport has generated the interface PrimeNumberService which has the same method as the original method inside PrimeNumberService at server side.

PrimeNumberServiceImplService has a no-argument constructor that will be used to construct the Service object

PrimeNumberServiceImplService has a method getPrimeNumberServiceImplPort () that returns instance of PrimeNumberService on which service methods has to be called.

Step 2

Client supported code is generated, we can write the client to consume the published web service in a more simple code

Create the simple client class and call the web service using above generated files

PrimeNumberClient.java

  1. package com.kb.ws.client ;

  2. public class PrimeNumberClient {

  3. public static void main( String [ ] args) {

  4.         PrimeNumberServiceImplService service = new PrimeNumberServiceImplService( ) ;

  5.         PrimeNumberService primeNumberService = service.getPrimeNumberServiceImplPort ( ) ;

  6. System.out.println (primeNumberService.isPrimeNumber ( 13 ) ) ;

  7. System.out.println (primeNumberService.isPrimeNumber ( 14 ) ) ;

  8. }

  9. }

package com.kb.ws.client;  public class PrimeNumberClient { 	public static void main(String[] args) { 		PrimeNumberServiceImplService service = new PrimeNumberServiceImplService(); 		PrimeNumberService primeNumberService = service.getPrimeNumberServiceImplPort(); 		          		 System.out.println(primeNumberService.isPrimeNumber(13)); 		  		 System.out.println(primeNumberService.isPrimeNumber(14));  	}  }


we can observe that getPrimeNumberServiceImplPort() method of PrimeNumberServiceImplService generated class has encapsulated the logic to get the server port from QName and serviceEndpointInterface and hence client code is very cleaner and simple.

Step 3

Run the client class
Make sure the service is up and running, refer previous article for publishing the service.

Setp 4

Output

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