Tutorial by Vinod Kumat

Tutorial webservices JDK6 wsgen wsimport utility


Note: before reading this, first of all read the initial tutorials : Web services
and assuming that you have installed the jdk1.6 or 1.7 or 1.8 and eclipse in your system, jdk 1.6 build path set in eclipse.

Let's start, Code first approach, web services tutorial with jdk, wsgen and wsimport utility.

wsimport - The wsimport tool generates JAX-WS portable artifacts used in JAX-WS clients and services. The tool reads a WSDL and generates all the required artifacts for web service development, deployment, and invocation (Used in Contract first approach and generate client side artifacts)

wsgen - The wsgen tool reads a service endpoint implementation class and generates all of the portable artifacts for a JAX-WS web service.(Used in code first approach)

Code first approach:
What we need to do in this approach, need to writhe service endpoint class and then expose this as web services.

Follow the following steps:

1. Create a Java Project in Eclipse.
Double click on the eclipse.exe located on desktop or run from programm file.
File > New > Project > Java > Java Project.







Enter the Project name: JAX-WebServices-Bottom-Up
Click Next then Finish.

Dir structure in project:
- JAX-WebServices-Bottom-Up
  - src
  - +JRE System Library [jre6] or higher  


2. Create a POJO java file
Create the java POJO class, while following POJO definition public access:
- The service endpoint class should public and should not Final and Abstract
- The service endpoint class must have a default public constructor
- The service endpoint class must not define the finalize method
- The service endpoint class must have a default public constructor and so on.

Right click on the src > New > Class
Enter the package name: com.vinod.tutorialwebservices
Enter the name: GreetingWebService
Click Finish.







3. Annotate your service endpoint class with JAX-WS annotations


GreetingWebService.java file look like as mention below, just we need to add some of JAXRS annotation for implementation of web services:

package com.vinod.tutorialwebservices;

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

@WebService(name = "GreetingWebService", targetNamespace = "http://www.tutorialwebservices.vinod.com")
public class GreetingWebService {
    @WebMethod
    public String sayWelcome(@WebParam(name = "sName") String sName) {
        return "Welcome " + sName;
    }
}

@WebService annotation is used for making GreetingWebService as web services, with name and targetNamespace.

@WebMethod: annotation is used for customizing the web services operation parameter and  used in WSDL.

4. Generate JAXWS artifacts using wsgen tool
Endpoint service class is ready. Now what we have to that is need to generate all of the portable artifacts for a JAX-WS web service. JDK6 have wsgen tool to generate all.

Black and white command need to perform, Go to the command prompt and fire wsgen command

F:\blogs\JAX-WebServices-Bottom-Up>wsgen -verbose -keep -wsdl -cp bin -s src com
.vinod.tutorialwebservices.GreetingWebService
Note:   ap round: 1
[ProcessedMethods Class: com.vinod.tutorialwebservices.GreetingWebService]
[should process method: sayWelcome hasWebMethods: true ]
[endpointReferencesInterface: false]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen - method: sayWelcome(java.lang.String)]
[method.getDeclaringType(): com.vinod.tutorialwebservices.GreetingWebService]
[requestWrapper: com.vinod.tutorialwebservices.jaxws.SayWelcome]
[ProcessedMethods Class: java.lang.Object]
com\vinod\tutorialwebservices\jaxws\SayWelcome.java
com\vinod\tutorialwebservices\jaxws\SayWelcomeResponse.java
Note:   ap round: 2

The wsgen command-line tool generates the necessary artifacts required for JAX-WS applications when starting from Java code. When using a bottoms-up approach to develop JAX-WS Web services and you are starting from a service endpoint implementation, use the wsgen tool to generate the required JAX-WS artifacts.
Meaning of wsgen Parameters

F:\blogs\JAX-WebServices-Bottom-Up>wsgen
Missing SEI
Usage: WSGEN [options] <SEI>

where [options] include:
  -classpath <path>          specify where to find input class files
  -cp <path>                 same as -classpath <path>
  -d <directory>             specify where to place generated output 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
  -keep                      keep generated files
  -r <directory>             resource destination directory, specify where to
                             place resouce files such as WSDLs
  -s <directory>             specify where to place generated source files
  -verbose                   output messages about what the compiler is doing
  -version                   print version information
  -wsdl[:protocol]           generate a WSDL file.  The protocol is optional.
                             Valid protocols are soap1.1 and Xsoap1.2, the default
                             is soap1.1.  Xsoap1.2 is not standard and can only be
                             used in conjunction with the -extension option
  -servicename <name>        specify the Service name to use in the generated WSDL
                             Used in conjunction with the -wsdl option.
  -portname <name>           specify the Port name to use in the generated WSDL
                             Used in conjunction with the -wsdl option.

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

File structure will there likewise below:














5. Expose your service
You know, we've nice method publish inside Endpoint class. Part of JDK, nothing is required except JDK.
Right click on the src folder > new > class.
Package name: com.vinod.tutorialwebservices.pbls
Name: GreetingWebServicePublish

package com.vinod.tutorialwebservices.pbls;

import javax.xml.ws.Endpoint;
import com.vinod.tutorialwebservices.GreetingWebService;

/**
 * @author Vinod Kumar
 *
 */
public class GreetingWebServicePublish {
/**
*
* @param ar
*/
public static void main(String ar[]) {
Endpoint.publish("http://localhost:8888/GreetingWebService", new GreetingWebService());
}
}

Note: This is the temporary solution for testing the web services in jdk environment. In real project won't be work. Need to deployed one of web server or application server.

6. How to verify the service
For verify the web service, need to run the our end point class.
Right Click on the GreetingWebServicePublish file > Run As > Java Application.
Our services will available for consume it with help of endpoint URL:
http://localhost:8888/GreetingWebService?WSDL

This URL we have define in GreetingWebServicePublish file.

Open the any browser then put http://localhost:8888/GreetingWebService?WSDL in address bar and hit enter.

  <?xml version="1.0" encoding="UTF-8" ?>
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. 
  -->
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. 
  -->

<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.tutorialwebservices.vinod.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.tutorialwebservices.vinod.com" name="GreetingWebServiceService">
<types>
<xsd:schema>
  <xsd:import namespace="http://www.tutorialwebservices.vinod.com" schemaLocation="http://localhost:8888/GreetingWebService?xsd=1" />
  </xsd:schema>
  </types>
<message name="sayWelcome">
  <part name="parameters" element="tns:sayWelcome" />
  </message>
<message name="sayWelcomeResponse">
  <part name="parameters" element="tns:sayWelcomeResponse" />
  </message>
<portType name="GreetingWebService">
<operation name="sayWelcome">
  <input message="tns:sayWelcome" />
  <output message="tns:sayWelcomeResponse" />
  </operation>
  </portType>
<binding name="GreetingWebServicePortBinding" type="tns:GreetingWebService">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name="sayWelcome">
  <soap:operation soapAction="" />
<input>
  <soap:body use="literal" />
  </input>
<output>
  <soap:body use="literal" />
  </output>
  </operation>
  </binding>
<service name="GreetingWebServiceService">
<port name="GreetingWebServicePort" binding="tns:GreetingWebServicePortBinding">
  <soap:address location="http://localhost:8888/GreetingWebService" />
</port>
</service>
</definitions>


How to do performance testing for web services (soap) by jmeter

Last Words:
Thanks! Hopping you have enjoy while reading this and did not face any issue. Put your input if any.
Happy learning and implementation.

2 comments:

  1. Hi I am getting this error while executing wsgen tool..
    exception in thread main java.lang.unsupportedclassversionerror unsupported major.minor version 51.0
    I am using Eclipse kelpar version JDK 1.6 and JRE 7 version
    please tell me the solution for this

    ReplyDelete
    Replies
    1. Change the JDK and JRE to same version.
      JRE 6 and JDK 6
      or
      JRE 7 and JDK 7

      J2SE 8 = 52,
      J2SE 7 = 51,
      J2SE 6.0 = 50,
      J2SE 5.0 = 49,
      JDK 1.4 = 48,
      JDK 1.3 = 47,
      JDK 1.2 = 46,
      JDK 1.1 = 45

      Fro more details see the http://www.tutorialbyexample.com

      Delete