JAX-WS: wsgen tool with Example

Connect with

Oracle Java

The wsgen utility tool available under ${JAVA_HOME}/bin/wsgen . The wsgen tool

  1. used to parse an existing web service implementation class and
  2. generates required files i.e. JAX-WS artifacts, for web service deployment.

1. What is wsgen tool?

wsgen tool is available in ${JAVA_HOME}/bin/ folder , in window wsgen.bat and in linux wsgen.sh file . This java utility tool is used to generate the artifact in java web services .

following is the help when you type wsgen on command line.

E:\jaxws>wsgen

where [options] include:
  -classpath           specify where to find input class files and wsgen extensions
  -cp                  specify where to find input class files and wsgen 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
  -J            pass this option to javac
  -keep                      keep generated files
  -r              resource destination directory, specify where to
                             place resouce files such as WSDLs
  -s              specify where to place generated source files
  -verbose                   output messages about what the compiler is doing
  -version                   print version information
  -fullversion               print full version information
  -wsdl[:protocol]           generate a WSDL file. The protocol is optional.
                             Valid protocols are [soap1.1, Xsoap1.2],
                             the default is soap1.1.
                             The non standard protocols [Xsoap1.2]
                             can only be used in conjunction with the
                             -extension option.
  -inlineSchemas             inline schemas in the generated wsdl. Must be
                             used in conjunction with the -wsdl option.
  -servicename         specify the Service name to use in the generated WSDL
                             Used in conjunction with the -wsdl option.
  -portname            specify the Port name to use in the generated WSDL
                             Used in conjunction with the -wsdl option.
  -x                   specify External Web Service Metadata xml descriptor

Extensions:
  -Xnocompile                do not compile generated Java files

Examples:
  wsgen -cp . example.Stock
  wsgen -cp . example.Stock -wsdl -servicename {http://mynamespace}MyService

2. Why wsgen tool required?

Usually we required wsgent utility tool to generate artifact. What typ of artifact are being generate is the anothe question.

  1. Generates JAX-WS portable artifacts (i.e. Java files) for web service deployment.
  2. Generates WSDL file and xsd files, for testing or web service client development.

3. web service class

 
package com.mysoftkey.jaxws.hellowold;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(endpointInterface = "com.mysoftkey.jaxws.hellowold.HelloWorld")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class HelloWorldImpl implements HelloWorld {

 @WebMethod()
 public String sayHelloWorld(String name) {
  return "Hello world JAX-WS, " + name;
 }

}

4. Generate JAX-WS portable artifact java file

browse upto project bin folder. I mean to say that go to hello

File: SayHelloWorld.java

 

package com.mysoftkey.jaxws.hellowold.jaxws;

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 = "sayHelloWorld", namespace = "http://hellowold.jaxws.mysoftkey.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHelloWorld", namespace = "http://hellowold.jaxws.mysoftkey.com/")
public class SayHelloWorld {

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

 public String getArg0() {
  return this.arg0;
 }

 public void setArg0(String arg0) {
  this.arg0 = arg0;
 }

}

File: SayHelloWorldResponse.java

 

package com.mysoftkey.jaxws.hellowold.jaxws;

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 = "sayHelloWorldResponse", namespace = "http://hellowold.jaxws.mysoftkey.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHelloWorldResponse", namespace = "http://hellowold.jaxws.mysoftkey.com/")
public class SayHelloWorldResponse {

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

 public String get_return() {
  return this._return;
 }

 public void set_return(String _return) {
  this._return = _return;
 }

}

5. Generate WSDL and XSD file using wsgen tool

copy com folder with .class of HelloWorldImpl.class in e:\jaxws\ folder and open a terminal and run

folder structure before generation

 
E:\jaxws>tree /f
Folder PATH listing for volume New Volume
Volume serial number is 00000011 D8C6:F3BB
E:.
└───com
    └───mysoftkey
        └───jaxws
            └───hellowold
                    HelloWorld.class
                    HelloWorldImpl.class

 
E:\jaxws>wsgen -cp . -verbose -keep com.mysoftkey.jaxws.hellowold.HelloWorldImpl -wsdl

folder structure after generation of wsdl and xsd artifact

 
E:\jaxws>tree /f
Folder PATH listing for volume New Volume
Volume serial number is 00000011 D8C6:F3BB
E:.
│   HelloWorldImplService.wsdl
│   HelloWorldImplService_schema1.xsd
│
└───com
    └───mysoftkey
        └───jaxws
            └───hellowold
                    HelloWorld.class
                    HelloWorldImpl.class

6. Generated wsdl file

File: HelloWorldImplService.wsdl

 



  
  
    
  
  
    
  
  
    
      
      
    
  
  
    
    
      
      
        
      
      
        
      
    
  
  
    
      
    
  



7. Generated XSD file

File: HelloWorldImplService_schema1.xsd

 



  

  



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


Connect with

Leave a Reply

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