Tuesday, January 3, 2012

JAX-WS 2.0


JAX-WS 2.0

Java™ API for XML Web Services

  • More document centric and replaces RPC programming model.
  • Dynamic proxy mechanism gives better platform independence.
  • Simplifies development using annotations.
  • More support to Asynchronous calls:
    • Polling model: Client gets response object back
    • Callback model: Client provides callback handler to process inbound response object.
It also supports synchronous calls.
  • Creates and initializes common resources in JRE.
  • Uses JAXB 2.0.
  • Provides dispatch (dynamic) client (XML messaging oriented client)
    • Payload data mode : Dispatch client only responsible for <soap:Body> contents, here JAX-WS adds the <soap:Envelop> and <soap:Header> elements.
    • Message data mode: Dispatch client is responsible for entire SOAP message.
  • Messge Transmission Optimized Mechanism (MTOM)
Can be used for binary attachments.
  • DataBindings :
    • SAAJ
    • JAXB
  • Support for SOAP 1.2
  • Wsgwn and wsimport command line tools.

Annotations:


Describe metadata of service.

Below are some JSR 181 annotations :

Annotation
Remark
Class
Attributes
@WebService
Mark  class / endpoint implementation as implementing a web service or to mark that a service endpoint interface as defining a web service interface.
javax.jws.WebService
·        name –Name of wsdl:portType. (Default value is unqualified name of the Java clss / interface)
·        targetNamespace – XML namespace of WSDL and elements generated from it.
  • serviceName – Service name of web service (wsdl:service). Default value class/interface name + ‘Service’.
  • endpointInterface – Qualified name of service endpoint interface that defines service’s.
  • portName – The wsdl:portName. Default value is WebService.name+’port’.
@WebMethod
Denotes a web service operation method.
javax.jws.WebMethod
  • operationName – Same as wsdl:operation . Default value is name of java method,
  • action – Action for this operation. SOAPAction. Default – name of method.
  • exclude – To exclude method from web service. Default value is false.
@Oneway
In case if method has only input message (parameter) and no output message (returns void).
javax.jws.Oneway


@WebParam
Maps single parameter to message part or element.
javax.jws.WebParam

·        name – name of wsdl:part. Default value is ‘argPosition’ where ‘Position’ is parameter position within a parameter list which starts from 0.
·        targetNamespace – XML namespace. Default is ‘targetNamespace’ for webservice
·        mode – Parameter direction for method. IN/INOUT and OUT are possible values. Default values are conditional as it will be IN for non-holders and INOUT for holder parameters.
·       header – Whether parameter should be carried in header. Default is False which will send parameter in message body.
·       partName – Name of wsdl:part. Used only with RPC or DOCUMENT/BARE.
@WebResult
Maps method return value to a WSDL part or XML element.
javax.jws.WebResult

·       name – Name of the return value as it is listed in the WSDL file as name of wsdl:part. Default value for RPC  and DOCUMENT / WRAPPED is ‘return’ and methodName+Response for DOCUMENT/BARE.
·       targetNamespace – XML namespace for return value.
·       Header – Whether the result should be carried in header. Default – False.
·       partName – Part name for the result. Default - @WebResult. name
@HandlerChain

Externally defined handeler chain.
javax.jws.HandlerChain

·       file – Location of file containing handler chain.
·       Name – The handler chain name from within the handler chain file.
@SOAPBinding
Specifies mapping of webservice onto SOAP message protocol.
javax.jws.SOAPBinding
·       style – Defines encoding style. Default - DOCUMENT
·       Use – Defines the formatting used for messages. Default – LITERAL.
·       parameterStyle - Determines if the method's parameters represent the entire message body or whether the parameters are wrapped in a body element named after the operation. Default – WRAPPED.

Developing JAX-WS webservice from java class (Bottom-up)

1.      RPC Style
§         Write Service Endpoint interface

package com.rtvk.services.rpc;

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface WeightUnitConversion {
                        @WebMethod public float kgsToPounds(float arg0)
throws java.rmi.RemoteException;
@WebMethod public float poundsToKgs(float arg0)
throws java.rmi.RemoteException;
}
§         Write class which implements Endpoint Service

package com.rtvk.services.rpc.impl;

import javax.jws.WebService;

import com.rtvk.services.rpc.WeightUnitConversion;

/**
 * Implementation of service.
 * @author rpandya
 */
@WebService(endpointInterface =
"com.rtvk.services.rpc.WeightUnitConversion")
public class WeightUnitConversionImpl
implements WeightUnitConversion{

      @Override
      public float kgsToPounds(float
weightInKgs) {
            return (weightInKgs * 2.2f);
      }

      @Override
      public float poundsToKgs(float
weightInPounds) {
return (weightInPounds /
2.2f);
      }

}
§         Build and deploy OR publish using javax.xml.ws.Endpoint
Here is ANT xml
<?xml  version="1.0" ?>
<project name="Axis2ServiceSample" default="war">
    <target name="init">
        <mkdir dir="build/classes"/>
            <mkdir dir="dist"/>
    </target>
      <path id="compile.classpath">
            <fileset dir="WebContent/WEB-
INF/lib">
                  <include  name="*.jar"/>
            </fileset> 
      </path>
      <target  name="compile" depends="init" >
<javac destdir="build/classes"
debug="true" srcdir="src"  includeantruntime="false">
            <classpath
refid="compile.classpath"/>
            </javac>
      </target>
      <target  name="war" depends="compile">
            <war
destfile="dist/Axis2ServiceSample.w
ar" webxml="WebContent/WEB-
INF/web.xml">
                  <fileset dir="WebContent"/>
                  <lib dir="WebContent/WEB-
INF/lib"/>
                  <classes

dir="build/classes"/>
            </war>
      </target>
    <target name="clean">
        <delete dir="build"/>
            <delete dir="dist"/>
    </target>
</project>

Deploy generated war.

§         Check services are deployed successfully.
You can check  wsdl of deployed services

                                   
                                    OR
http://localhost:8080/Axis2ServiceSample/weightUnitConversion          
will give you details in tabular form

Web Services

Endpoint
Information
Service Name:
{http://impl.rpc.services.rtvk.com/}WeightUnitConversionImplService
Port Name:
{http://impl.rpc.services.rtvk.com/}WeightUnitConversionImplPort
Address:
Axis2ServiceSample/
weightUnitConversion
WSDL:
Implementation class:
com.rtvk.services.rpc.impl.
WeightUnitConversionImpl

§         Now write client to call RPCService : Here is an example
package userguide.clients;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

/**
 * The Class WeightConvertorClient.
 * @author rpandya
 */
public class WeightConvertorClient {
      public static void main(String[] args) throws Exception {
            try{
        RPCServiceClient serviceClient = new RPCServiceClient();

        Options options = serviceClient.getOptions();

        EndpointReference targetEPR = new EndpointReference(
                "http://localhost:8080/Axis2ServiceSample/weightUnitConversion");
        options.setTo(targetEPR);

        QName weightUnitConversionImplService =
            new QName("http://rpc.services.rtvk.com/", "kgsToPounds");

        Float[] weightUnitConversionArgs = new Float[] { 80f};
        Class[] returnTypes = new Class[] { Float.class };
       
        Object[] response = serviceClient.invokeBlocking(weightUnitConversionImplService,
                  weightUnitConversionArgs, returnTypes);
        System.out.println(response[0]);
            }catch(AxisFault axisFault){
                  axisFault.printStackTrace();
            }
      }

}

§         Run Client and Check output.

No comments: