JAX-WS Stand Alone Web Services Using JDK1.6 Wsgen Wsimport Utility


Video: 




Open the Eclipse.
File > New > Java Project
Enter the project name : Stand-Alone-Web-Services-Using-JDK6-Wsgen-Wsimport-Utility
Click on the next button

Click on the finish





Right click on the src folder under project Stand-Alone-Web-Services-Using-JDK6-Wsgen-Wsimport-Utility.
New > Class.



Enter package name: com.tutorialbyexample
Name of Class: MathService

package com.tutorialbyexample;

public class MathService {

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

       public double add(double a, double b) {
              return a + b;

       }

       public double sub(double a, double b) {
              return a - b;

       }
}


Now we have to implement the annotation and have to implement it for achieving the Jax web services functionality.
After annotation its look likes:

package com.tutorialbyexample;

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

@WebService(name = "MathService", targetNamespace = "http://www.tutorialbyexample.com/MathService")
public class MathService {

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

       @WebMethod
       public double add(@WebParam(name = "a") double a, @WebParam(name = "b") double b)        {
              return a + b;

       }

       @WebMethod
       public double sub(@WebParam(name = "a") double a, @WebParam(name = "b") double b)       {
              return a - b;

       }
}


Create the resource folder under src dir.

Open the command prompt:

Generate the JaxWS supported file by 

c:\workspace\Stand-Alone-Web-Services-Using-JDK6-Wsgen-Wsimport-Utility>
wsgen -keep –classpath  class -cp class -d src -r src\resource -verbose -wsdl com.tutorialbyexample.MathService





Right click on the src folder and create new class JaxWSPublish.java for publishing the JaxWS:

Enter the package name: com.tutorialbyexample.client
Enter the name: JaxWSPublish
Click finish

package com.tutorialbyexample.client;

import javax.xml.ws.Endpoint;

import com.tutorialbyexample.MathService;

public class JaxWSPublish {

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

       public static void main(String[] args) {
              Endpoint.publish("http://localhost:8888/webservice/www.tutorialbyexample.com", new MathService());

              System.out.println("MathService has been published!");

       }

}


Generated files:

Add.java:

package com.tutorialbyexample.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 = "add", namespace = "http://www.tutorialbyexample.com/MathService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "add", namespace = "http://www.tutorialbyexample.com/MathService", propOrder = {
    "a",
    "b"
})
public class Add {

    @XmlElement(name = "a", namespace = "")
    private double a;
    @XmlElement(name = "b", namespace = "")
    private double b;

    /**
     *
     * @return
     *     returns double
     */
    public double getA() {
        return this.a;
    }

    /**
     *
     * @param a
     *     the value for the a property
     */
    public void setA(double a) {
        this.a = a;
    }

    /**
     *
     * @return
     *     returns double
     */
    public double getB() {
        return this.b;
    }

    /**
     *
     * @param b
     *     the value for the b property
     */
    public void setB(double b) {
        this.b = b;
    }


}

AddResource.java

package com.tutorialbyexample.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 = "addResponse", namespace = "http://www.tutorialbyexample.com/MathService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "addResponse", namespace = "http://www.tutorialbyexample.com/MathService")
public class AddResponse {

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

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

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

}

sub.java

package com.tutorialbyexample.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 = "sub", namespace = "http://www.tutorialbyexample.com/MathService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sub", namespace = "http://www.tutorialbyexample.com/MathService", propOrder = {
    "a",
    "b"
})
public class Sub {

    @XmlElement(name = "a", namespace = "")
    private double a;
    @XmlElement(name = "b", namespace = "")
    private double b;

    /**
     *
     * @return
     *     returns double
     */
    public double getA() {
        return this.a;
    }

    /**
     *
     * @param a
     *     the value for the a property
     */
    public void setA(double a) {
        this.a = a;
    }

    /**
     *
     * @return
     *     returns double
     */
    public double getB() {
        return this.b;
    }

    /**
     *
     * @param b
     *     the value for the b property
     */
    public void setB(double b) {
        this.b = b;
    }

}


SubResource.java

package com.tutorialbyexample.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 = "subResponse", namespace = "http://www.tutorialbyexample.com/MathService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "subResponse", namespace = "http://www.tutorialbyexample.com/MathService")
public class SubResponse {

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

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

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

}


MathServiceService_schema1.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://www.tutorialbyexample.com/MathService" xmlns:tns="http://www.tutorialbyexample.com/MathService" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="add" type="tns:add"/>

  <xs:element name="addResponse" type="tns:addResponse"/>

  <xs:element name="sub" type="tns:sub"/>

  <xs:element name="subResponse" type="tns:subResponse"/>

  <xs:complexType name="add">
    <xs:sequence>
      <xs:element name="a" type="xs:double"/>
      <xs:element name="b" type="xs:double"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="addResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:double"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sub">
    <xs:sequence>
      <xs:element name="a" type="xs:double"/>
      <xs:element name="b" type="xs:double"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="subResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:double"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>


MathServiceService.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://www.tutorialbyexample.com/MathService" xmlns:tns="http://www.tutorialbyexample.com/MathService" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="add" type="tns:add"/>

  <xs:element name="addResponse" type="tns:addResponse"/>

  <xs:element name="sub" type="tns:sub"/>

  <xs:element name="subResponse" type="tns:subResponse"/>

  <xs:complexType name="add">
    <xs:sequence>
      <xs:element name="a" type="xs:double"/>
      <xs:element name="b" type="xs:double"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="addResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:double"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sub">
    <xs:sequence>
      <xs:element name="a" type="xs:double"/>
      <xs:element name="b" type="xs:double"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="subResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:double"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>



Run the Publish class:
Double click on the JaxWSPublish
or
Right click on the JaxWSPublish > Run > Java Application

In console you will get:
MathService has been published!

Now open the browser and type:

http://localhost:8888/webservice/www.tutorialbyexample.com



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.tutorialbyexample.com/MathService" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"targetNamespace="http://www.tutorialbyexample.com/MathService" name="MathServiceService">

<types>

<xsd:schema>

<xsd:import namespace="http://www.tutorialbyexample.com/MathService" schemaLocation="http://localhost:8888/webservice/www.tutorialbyexample.com?xsd=1"/>
</xsd:schema>
</types>

<message name="add">

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

<message name="addResponse">

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

<message name="sub">

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

<message name="subResponse">

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

<portType name="MathService">

<operation name="add">

<input wsam:Action="http://www.tutorialbyexample.com/MathService/MathService/addRequest" message="tns:add"/>

<output wsam:Action="http://www.tutorialbyexample.com/MathService/MathService/addResponse" message="tns:addResponse"/>
</operation>

<operation name="sub">

<input wsam:Action="http://www.tutorialbyexample.com/MathService/MathService/subRequest" message="tns:sub"/>

<output wsam:Action="http://www.tutorialbyexample.com/MathService/MathService/subResponse" message="tns:subResponse"/>
</operation>
</portType>

<binding name="MathServicePortBinding" type="tns:MathService">

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

<operation name="add">

<soap:operation soapAction=""/>

<input>

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

<output>

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

<operation name="sub">

<soap:operation soapAction=""/>

<input>

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

<output>

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

<service name="MathServiceService">

<port name="MathServicePort" binding="tns:MathServicePortBinding">

<soap:address location="http://localhost:8888/webservice/www.tutorialbyexample.com"/>
</port>
</service>
</definitions>

How to write client code : Click


Done from my end, try at your end and put your point of view if any.

Have good day ahead!!!



No comments:

Post a Comment

Please provide your input, it will honer for me...