01 - JAX WS Introduction

Introduction

1.1 JAX WS:

Java API for XML Web Services (JAX WS) is a technology used to create XML based web services. Two different applications are able to communicate by creating web services and clients. JAX WS are built on top of JAX RPC, which is used for remote procedure call. SOAP is an XML based protocol which represents the remote procedure call in JAX WS

1.2  Version history:

JAX WS 2.1/2.2—Java SE release 7.0 includes JAX WS 2.2, JAX WS 2.0

This one is the first version of JAX WS and introduced in J2EE 5.0- replaced JAX RPC

1.3  JAX WS in SOA:

Web Services have a major role in SOA  Applications. Web services support interoperability between different platforms using XML based standards like WSDL, SOAP and UDDI. By using these standards, web services can be registered, defined and published

1.4 Standards and Specification

JAX WS is based on JSR 224

       WS-I Basic profile

       WS-I Attachments profile

       WS-I Simple SOAP Binding Profile

       WS- Addressing

1.5 Components

JAX WS based Web services are built by using the following components:

1.5.1 SOAP

SOAP stands for Simple Object Access protocol, SOAP is a message oriented protocol used for transferring/transmitting the data between two distinct systems

In JAX WS web services, SOAP messages are used to carry the information for end to end transmission over HTTP. We can achieve the following functions using SOAP protocol:

SOAP message contains the following three parts:

SOAP message contains an Envelope element which carries the meta-data information about the message. This Envelope element contains an optional Header element and a mandatory Body element

  • Envelope

SOAP message contains the information which explains how the message is to be processed

  • Heaer

Body part of the SOAP message contains the actual payload of the end to end message transmission

  • Body

1.5.2 WSDL

WSDL stands for Web Services Description Language.

This XML based language helps to describe and define the web services

Any service consumer can communicate through WSDL to obtain their required service.

WSDL describes the following information:

The name of the service is the function name which is exposed as a service

  • Name of the Service

The path of the service gives the end point address information about the service

  • Path of the service

Request and response message format

  • The type of the transmission protocol used
  • Data type of the input messages

Elements of WSDL specification:

Definition:

The definition element must be the root element of the WSDL specification.

This element defines the name of the web service, information about the multiple namespaces used though out the WSDL document.

Types:

This element is very important while sharing the data across multiple platforms using web services. The data type of the variables we are using in the web services should be compatible with all platforms. The types element is used to define all the data types used between the end to end transmission. XSD or XML Schema Definition is used to define the types used in the WSDL. The detailed overview of XSD will explain in the next section

Message:

Each message describes either the input or output of the web service transmission and it has the name of the message and zero or more part message elements Input message contains the request information and the corresponding part message details. The output message element contains the details about the response from the server

portType

PortType elements are an entity like a Java class containing group of operations.

This element describes a complete operation definition by combining input and output messages. One porttype element can describe multiple operations

binding

The binding element describes how the service will be implemented in the transmission.

Service

Service element provides the address, information about the service. This element defines the URL of the service we are invoking

1.5.3 XML schema Definition

         XSD (XML Schema Definitions)
        The most commonly used XSD data types in wsdl are listed as below:
        xs:string
        xs:decimal
        xs:integer
        xs:boolean
        xs:date
        xs:time


Attributes:

Attributes are used to define the type of the element with additional information
Example:

       <xs:attribute name=”productId”  type”xs:integer”/>

Simple elements do not have attributes
If we define attribute in an element, the element should be complex type
Attribute definition can be made optional or mandatory by using ‘use’ element
Example:

<xs:attribute name=”productId” type”xs:integer” use=”optional”/>
    <xs:attribute name=”productId” type”xs:integer” use”required”/>

Elements:
XSD elements can be simple elements or complex elements
A simple element is an xml element which contains only text related content like string, date and  boolean.
Simple elements do not contain other elements or attributes
Example:

<xs:element name=”id” type=”xs:integer”/>
<xs:element name="name" type=”xs:string”/>
<xs:element name="purchasedate" type="xs:date"/>

The XML for the above example can be as below:

 <id>1001</id>
<name>John</name>
<purchasedate>2014-10-10</purchasedate>

Elements with restriction
Elements can have default or fixed values
Example

<xs:element name=”productType” type=”xs:string” default=”toys”/>

<xs:element name=”pricemargin” type=”xs:decimal” fixed=”0.30”/>


Complex elements:

Complex elements are elements which contain other elements or attribute
Example: Complex element with attribute

<xs:element name=”product”>
        <xs:complexType>
        <xs:attribute  name="productID"  type="xs:int"  use="required"/>
        </xs:complexType>
</xs:element>

The xml for the xsd as below:
<product productid=”1001”>
</product>

Example: Complex element with attribute and element

<xs:element name=”product”>
        <xs:complexType>
             <xs:attribute  name="productID"  type="xs:int"  use="required"/>
             <xs:element name=”productName” type=”xs:string”/>
             <xs:element name=”productType” type=”xs:string”/>
        </xs:complexType>
</xs:element>

MinOccurs and MaxOccurs
The number of occurrences of any element can be defined using miniatures and maxOccurs attributes. By default, the value of minOccurs and
maxOccurs is 1 and one element present.
Example

       <xs:element name=”product” minOccurs=”0” maxOccurs=”unbounded”/>
Restriction:
If any element needs to map to the specific pattern value, the restriction element can be used.  Restriction on String data type is as below:

   enumeration
    length
    maxLength
    minLength
    pattern
    whiteSpace

Example

<xs:element name="productCode">
       <xs:simpleType>
           <xs:restriction base="xs:string">
                   <pattern value="[A-Z][0-9][0-9][0-9]"/>
           </xs:restriction>
        </xs:simpleType>
</xs:element>

Handling Whitespace:

Whitespace characters like tabs, line feeds and carriage returns on element value can be handled as below
preserve:
Preserve restriction will not remove any whitespace characters

<xs:element name="productSummary">
          <xs:simpleType>
                 <xs:restriction base="xs:string">
                          <xs:whiteSpace value="preserve"/>
                 </xs:restriction>
            </xs:simpleType>
</xs:element>

replace
With this restriction, all the whitespace characters are replaced with spaces

<xs:element name="productSummary">
      <xs:simpleType>
             <xs:restriction base="xs:string">
                    <xs:whiteSpace value="replace"/>
             </xs:restriction>
       </xs:simpleType>
</xs:element>

collapse

<xs:element name="productSummary">
        <xs:simpleType>
               <xs:restriction base="xs:string">
                    <xs:whiteSpace value="collapse"/>
                </xs:restriction>
         </xs:simpleType>
</xs:element>


With collapse restriction, all the whitespace in the element like leading and trailing spaces will be removed

<xs:element name="productSummary">
       <xs:simpleType>
            <xs:restriction base="xs:string">
                 <xs:whiteSpace value="preserve"/>
            </xs:restriction>
       </xs:simpleType>
</xs:element>

An element value can be restricted to a range of values
Example

<xs:element name="qty">
     <xs:simpleType>
            <xs:restriction base="xs:integer">
                 <xs:minInclusive value="10"/>
                 <xs:maxInclusive value="50"/>
            </xs:restriction>
      </xs:simpleType>
</xs:element>


Time and Timezone:

Time is specified in the form hh:mm:ss

 
<xs:element name="arrivalTime" type="xs:time"/>
<arrivalTime>09:00:00</arrivalTime>

Timezone in UTC format is specified as below:

       <arrivalTime>09:00:00Z</arrivalTime>
 

1.5.d JAX RPC

JAX RPC stands for Java API for XML based Remote Procedure Call is used for building  web services and clients. With JAX RPC, we do not require to generate or parse the SOAP messages. The JAX RPC run time coverts the API calls and responses to and from SOAP messages 

1.5.e JAXB

JAXB stands for Java Architecture for XML Binding is a technology used for binding XML and Java representation XML. This binding objective is achieved by two activities

1. Marshalling: The conversion from  Java object to XML document marshalling

2. Unmarshalling: The conversion from XML document into Java object is called Unmarshalling

1.5.f UDDI

The acronym for UDDI is Universal Description, Discovery and Integration. UDDI acts like a registry where we can describe our services. Any service consumer interested in those services can search in UDDI.UDDI uses WSDL to describe the service

1.6 Annotations used in JAX WS

Annotations can be used in Java classes in creating web services. The most commonly used annotations are listed below:

  • @WebService
  • @WebMethod
  • @WebParam
  • @WebResult
  • @SOAPBinding
  • @OneWay
  • @HandlerChain

@WebService and @WebMethod:

A simple java class with public method can be converted into web service by adding these annotations to class, methods with meta data.

Web Service annotation is used to define the web service at the class level

Web Method is used to expose a public operation of the web service

Examples:

@javax.jws.WebService
public class MyServiceImpl implements MyService{
    @javax.jws.WebMethod
    public void MyMethod{
    }
}


WebMethod Example:

@javax.jws.WebService(serviceName="MyService") 
public class MyServiceImpl implements MyService{
   @javax.jws.WebMethod
   public void MyMethod(){
   }
}

 

Web Method- operationName:

@javax.jws.WebService(serviceName="MyService") 
public class MyServiceImpl implements MyService{
    @javax.jws.WebMethod(operationName="myMethod") 
    public void myMethod(){
    } 
} 


The element operationName is used to explicitly declare the name of the Web Service method. If this element is not mentioned, the method will be named after the method name.

@javax.jws.WebService(serviceName="MyService") 
public class MyServiceImpl implements MyService{
    @javax.jws.WebMethod(operationName="myMethod") 
    public void myMethod(){
    }
    @javax.jws.WebMethod(exclude=true) 
    public void anotherMethod(){
    }
}


The element excludes in Web Method annotation is used to define whether this method needs to be published on the Web Service or not. Setting 'true' to this method excludes publishing into Web Service.

javax.jws.WebParam:

WebParam annotation is used to Customize the mapping between operation input parameters of the Web Service and elements of the generated WSDL file. The name of the parameters, targetNamespace and mode of the parameters(IN,OUT or INOUT) can be specified in WebParam

Example:

@javax.jws.WebService(serviceName="MyService") 
public class MyServiceImpl implements MyService{
    @javax.jws.WebMethod(operationName="myMethod") 
    public void myMethod(
        @WebParam(name="inputOne",
               targetNamespace="http://com.example.org/customerService") String inputOne)
    {
    }
}

javax.jws.WebResult:

WebResult annotation is used to customize the mapping between the Web Service operation return value and the corresponding element of the generated WSDL file.

@javax.jws.WebService(serviceName="MyService") 
public class MyServiceImpl implements MyService{
    @javax.jws.WebMethod(operationName="myMethod") 
    @WebResult(name="CustomerObj",
             targetNamespace="http://com.example/customerService")    
            public Customer myMethod(@WebParam(name="customeName",
               targetNamespace="http://com.example.org/customerService")   String name)
    {
       }
} 


javax.jws.SOAPBinding

SOAPBinding annotation is used define the mapping of the Web Service onto the SOAP message protocol.

Example

@javax.jws.WebService(serviceName="MyService") 
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,
             use=SOAPBinding.Use.Encoded, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public class MyServiceImpl implements MyService{
}

 

javax.jws.HandlerChain

HandlerChain is used to Associates a Web Service with handler chain configuration file

Example:

@javax.jws.WebService(serviceName="MyService") 
@javax.jws.HandlerChain(file=”HandlerChain.xml”, name=”CustomerHandler”)
public class HandlerChainImpl{
}


javax.jws.Oneway:

One-way annotation is used to define the method which has only input parameters without a return value. This annotation should be used along with the @WebMethod annotation only. If this annotation is used on a method which return a value other void will throw exceptions. This annotation does not have any attributes.

public class MyServiceImpl implements MyService{
    @javax.jws.WebMethod
    @javax.jws.OneWay
    public void myMethod(){
} 
  1. Message Exchange Patterns

There are four Message Exchange Patterns are used in Web Services

  • One Way: A message from the Client will be sent to Service and the service will not return any response
  •  Request -Response: A message reaches Service and the service returns a response to the client
  • Solicit Response: This is just opposite in direction with Request -Response. The service sends a message to the client and get a message back from the client
  • Notification: Service sends a message to the client and receives nothing in the response

Like us on Facebook