JAX-WS: wsimport tool example with step by step

Connect with

Oracle Java In JAX-WS you can generate the server side artifact and client side artifact of webservices using wsdl. It is your choice , how you start of developing webservice either Top-down ( contract-first ) or Bottom UP ( java code first). Which is better is better approach is completly depend on your preference and nature of services.

1. What is wsimport tool in JAX-WS?

wsimport tool help on command line
type: wsimport –help

E:\>cd mysoftkey

E:\mysoftkey>wsimport --hlep
unrecognized parameter --hlep


Usage: wsimport [options] 

where [options] include:
  -b                  specify jaxws/jaxb binding files or additional schemas
                            (Each  must have its own -b)
  -B            Pass this option to JAXB schema compiler
  -catalog            specify catalog file to resolve external entity references
                            supports TR9401, XCatalog, and OASIS XML Catalog format.
  -classpath          specify where to find user class files and wsimport extensions
  -cp                 specify where to find user class files and wsimport extensions
  -d             specify where to place generated output files
  -encoding       specify character encoding used by source files
  -extension                allow vendor extensions - functionality not specified
                            by the specification.  Use of extensions may
                            result in applications that are not portable or
                            may not interoperate with other implementations
  -help                     display help
  -httpproxy:        set a HTTP proxy. Format is [user[:password]@]proxyHost:proxyPort
                            (port defaults to 8080)
  -J           pass this option to javac
  -keep                     keep generated files
  -p                   specifies the target package
  -quiet                    suppress wsimport output
  -s             specify where to place generated source files
  -target          generate code as per the given JAXWS spec version
                            Defaults to 2.2, Accepted values are 2.0, 2.1 and 2.2
                            e.g. 2.0 will generate compliant code for JAXWS 2.0 spec
  -verbose                  output messages about what the compiler is doing
  -version                  print version information
  -fullversion              print full version information
  -wsdllocation   @WebServiceClient.wsdlLocation value
  -clientjar       creates the jar file of the generated artifacts along with the
                            WSDL metadata required for invoking the web service.
  -generateJWS              generate stubbed JWS implementation file
  -implDestDir   specify where to generate JWS implementation file
  -implServiceName    local portion of service name for generated JWS implementation
  -implPortName       local portion of port name for generated JWS implementation

Extensions:
  -XadditionalHeaders              map headers not bound to request or response message to
                                   Java method parameters
  -Xauthfile                       file to carry authorization information in the format
                                   http://username:password@example.org/stock?wsdl
  -Xdebug                          print debug information
  -Xno-addressing-databinding      enable binding of W3C EndpointReferenceType to Java
  -Xnocompile                      do not compile generated Java files
  -XdisableAuthenticator           disable Authenticator used by JAX-WS RI,
                                   -Xauthfile option will be ignored if set
  -XdisableSSLHostnameVerification disable the SSL Hostname verification while fetching
                                   wsdls

Examples:
  wsimport stock.wsdl -b stock.xml -b stock.xjb
  wsimport -d generated http://example.org/stock?wsdl

E:\mysoftkey>

2. Why wsimport tool?

wsimport tool is used for 2 different purpose:

  1. To generate the client side artifact ( source code) to invoke webservices and
  2. To generate server side artifact to expose webservice in Top-Down approach (contract-first) webservice.

3. Client Side Artifact: Generate using wsdl

To generate the client side artifact using existing wsdl which is exposed as web service. You can access in browser before calling from terminal.

For example, we have a hellow webservice exposed/implemented and wsdl URL: http://localhost:8080/HelloWorldWS/HelloWorldWS?wsdl

command:

wsimport -keep -verbose http://localhost:8080/HelloWorldWS/HelloWorldWS?wsdl

console output

E:\mysoftkey>wsimport -keep -verbose http://localhost:8080/HelloWorldWS/HelloWorldWS?wsdl
parsing WSDL...


Generating code...

com\mysoftkey\jaxws\hellowold\HelloWorldImpl.java
com\mysoftkey\jaxws\hellowold\HelloWorldImplService.java

Compiling code...

javac -d E:\mysoftkey\. -classpath C:\Program Files\Java\jdk1.8.0_77/lib/tools.jar;C:\Program Files\Java\jdk1.8.0_77/classes -Xbootclasspath/p:C:\Program Files\Java\jdk1.8.0_77\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\rt.jar E:\mysoftkey\.\com\mysoftkey\jaxws\hellowold\HelloWorldImpl.java E:\mysoftkey\.\com\mysoftkey\jaxws\hellowold\HelloWorldImplService.java

directory structur of generated client source code

E:\mysoftkey:.
└───com
    └───mysoftkey
        └───jaxws
            └───hellowold
                    HelloWorldImpl.class
                    HelloWorldImpl.java
                    HelloWorldImplService.class
                    HelloWorldImplService.java

Copy Generated source code (com folder) in your Eclipse IDE or any IDE of your choice in client project under src folder. Now you are ready to write client by using this, you can visit another post how to write Java webservice client using WSDL-

4. Server Side Artifact: using wsdl

WSDL is basically used to generate the serverside artifact using wsimport. I wills how later this section.


5. Reference

wsimport tool

Your comments are welcome to improve this post. Happy Learing 🙂


Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *