JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach


In this session, we will learn, how to write the java web services with help of wsgen utility in jdk1.6+ and JAX-WS. 

What we need before start the code, I'm using as mention below and already installed in my system:
1. JDK1.6 or +
2. Eclipse
3. Windows OS
4. Basic knowledge of java

Let's start:




Verify the JDK installation

Open the command prompt and verify the java installation
Type the 
C:\>javac -version
javac 1.7.0_25

C:\>java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) Client VM (build 23.25-b01, mixed mode, sharing)

Now java installation got verified, as installed JDK1.7.0_25

Verify the Eclipse installation

Go the the installation directory of eclipse and click on the eclipse.exe
e.g C:\eclipse\eclipse-java-mars-1-win32\eclipse\eclipse.exe


JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach

Eclipse installation got verified.

Open the Eclipse

Create the new java project
File
New
Java Project
Type the name of project: 
JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach

JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach


Click on the Next, 
Change the bin to class as mention bellow image.
JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach-bin-class

Click Finish.

Project structure look like
\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach
        - src
        - JRE System Library[JavaSE-1.7]

Now, new java project creation done in Eclipse

Write the Service Class, AToZExamplesService.java

Right click on the src under project name \JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach.
New
Class
Type the name of package: com.atozexamples
Type the name of Class: AToZExamplesServices
JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach-Service-Class-atozexamples

Click the Finish

package com.atozexamples;

public class AToZExamplesService {

       public AToZExamplesService() {
              // TODO Auto-generated constructor stub
       }

}

Annotate service endpoint class with JAX-WS annotations

AToZExamplesService.java


Must have to follow certain rules at the time of creating the service endpoint class
- 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

package com.atozexamples;

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

@WebService(name = "AToZExamplesService", targetNamespace = "http://atozexample.com/AToZExamplesService")
public class AToZExamplesService {

       public AToZExamplesService() {
              // TODO Auto-generated constructor stub
       }

       @WebMethod
       public String printGreeting() {
              return "Hello from AToZExamples.com!!";
       }

}

Service end point class got annotated and added new method printGreeting()

Generate JAXWS artifacts using wsgen tool

Endpoint service class is ready. Now you have to generate all of the portable artifacts for a JAX-WS web service. JDK1.6 have wsgen tool to generate all this stuff.

Go to the command prompt and fire wsgen command

C:\>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, Xsoap1.2],
                             the default is soap1.1.
                             The non stanadard 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 <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


C:\>


Resource folder

Create the resource folder under src located in \JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach

Right click on the src
New
Folder
Name of Folder: resource
Finish

Change the path to your current project directory.
C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach
  - src
    - resource
  - JRE

Project directory look like:


C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach>dir
Volume in drive C is ISO1
Volume Serial Number is 2AB1-ADF2

Directory of C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach

14/02  09:47    <DIR>          .
14/02  09:47    <DIR>          ..
14/02  09:47               303 .classpath
14/02  09:44               473 .project
14/02  09:44    <DIR>          .settings
14/02  10:20    <DIR>          class
14/02  10:20    <DIR>          src
               2 File(s)            776 bytes

Now generate the jax-ws artificate with wsgen utility

C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach>wsgen -keep -classpath class -cp class -d src -r src\resource -verbose -wsdl com.atozexamples.AToZExamplesService

warning: The apt tool and its associated API are planned to be
removed in the next major JDK release.  These features have been
superseded by javac and the standardized annotation processing API,
javax.annotation.processing and javax.lang.model.  Users are
recommended to migrate to the annotation processing features of
javac; see the javac man page for more information.
Note:   ap round: 1
[ProcessedMethods Class: com.atozexamples.AToZExamplesService]
[should process method: printGreeting hasWebMethods: true ]
[endpointReferencesInterface: false]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen - method: printGreeting()]
[method.getDeclaringType(): com.atozexamples.AToZExamplesService]
[requestWrapper: com.atozexamples.jaxws.PrintGreeting]
[ProcessedMethods Class: java.lang.Object]
com\atozexamples\jaxws\PrintGreeting.java
com\atozexamples\jaxws\PrintGreetingResponse.java
Note:   ap round: 2

C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach>

JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach-Artifict-files

Services class and artifices file generate with help of wsgen utility.

PrintGreeting.java

package com.atozexamples.jaxws;

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

@XmlRootElement(name = "printGreeting", namespace = "http://www.atozexamples.com/AToZExamplesService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "printGreeting", namespace = "http://www.atozexamples.com/AToZExamplesService")
public class PrintGreeting {

}

PrintGreetingResponse.java

package com.atozexamples.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 = "printGreetingResponse", namespace = "http://www.atozexamples.com/AToZExamplesService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "printGreetingResponse", namespace = "http://www.atozexamples.com/AToZExamplesService")
public class PrintGreetingResponse {

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

       /**
        *
        * @return returns String
        */
       public String getReturn() {
              return this._return;
       }

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

}


Expose your service

For publish our service class to any container. In JDK itself we have EndPoint class with publish method.

We have to do new PublishAToZExamplesService

Right click on the src folder under current project.
New
Class
Type the name of class: PublishAToZExamplesService
Type the package name: com.atozexamples.publish
Check the: public static void main(String args[])
Click the Finish

package com.atozexamples.publish;

import javax.xml.ws.Endpoint;

import com.atozexamples.AToZExamplesService;

public class PublishAToZExamplesService {

       public PublishAToZExamplesService() {

       }

       public static void main(String[] args) {
              Endpoint.publish("http://localhost:8088/AToZExamplesService",
                  new AToZExamplesService());
       }
}

Run the PublishAToZExamplesService file

Right click on the PublishAToZExamplesService file
Run as
Java applicaiton


Verify the service

Open the IE or any browser
Enter the URL as http://localhost:8088/AToZExamplesService

JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach--Verify

Click on the WSDL link http://localhost:8088/AToZExamplesService?wsdl



This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<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://www.atozexamples.com/AToZExamplesService" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"targetNamespace="http://www.atozexamples.com/AToZExamplesService" name="AToZExamplesServiceService">

<types>

<xsd:schema>

<xsd:import namespace="http://www.atozexamples.com/AToZExamplesService" schemaLocation="http://localhost:8088/AToZExamplesService?xsd=1"/>
</xsd:schema>
</types>

<message name="printGreeting">

<part name="parameters" element="tns:printGreeting"/>
</message>

<message name="printGreetingResponse">

<part name="parameters" element="tns:printGreetingResponse"/>
</message>

<portType name="AToZExamplesService">

<operation name="printGreeting">

<input wsam:Action="http://www.atozexamples.com/AToZExamplesService/AToZExamplesService/printGreetingRequest" message="tns:printGreeting"/>

<output wsam:Action="http://www.atozexamples.com/AToZExamplesService/AToZExamplesService/printGreetingResponse" message="tns:printGreetingResponse"/>
</operation>
</portType>

<binding name="AToZExamplesServicePortBinding" type="tns:AToZExamplesService">

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

<operation name="printGreeting">

<soap:operation soapAction=""/>

<input>

<soap:body use="literal"/>
</input>

<output>

<soap:body use="literal"/>
</output>
</operation>
</binding>

<service name="AToZExamplesServiceService">

<port name="AToZExamplesServicePort" binding="tns:AToZExamplesServicePortBinding">

<soap:address location="http://localhost:8088/AToZExamplesService"/>
</port>
</service>
</definitions>


That all about jax-ws service in java with wsgen utility.
Next tutorial we will learn how to generate the client by wsimport utility.


Please like and share it.

No comments:

Post a Comment