You are on page 1of 59

Java Web Service

Objective
– What Is a Web Service?
– Identifying Web Service Standards
– JEE Web Service APIs
– Using Java API for XML based-WS API (JAX-WS)
– Developing a Web Service by Using the Bottom-
Up Approach
– Invoking the Web Service with a Java Client
Application
– Developing a Web Service by Using the Top-
Down Approach
What is Service ?
• A service is a behavior that is provided by a
component for use by any other component
based only on the interface contract.
• A service has a network-addressable interface.
• A service may be dynamically discovered and
used.
Service-oriented Architecture
• SOA is a design and a way of thinking about building s/w components.
Benefits of SOA
• Better reuse
– Build new client functionality on top of existing
Business Services
• Well defined interfaces
– Make changes without affecting clients
• Easier to maintain
– Changes/Versions are not all-or-nothing
• Better flexibility
What is a Web Service?

• In simpler terms…
– Software that performs a task.
• Sits on a network so other systems can talk to
it (most often SOAP over HTTP).
• Has a defined, published interface (via WSDL).
• Is platform and language independent.
• Typically uses XML.
Web service standards
Web services programming stack.

• Web service stack is a collection of


standardized protocols and application
programming interfaces (APIs) that lets
individuals and applications locate and utilize
Web services.
Simple Object Access Protocol

• SOAP is a lightweight protocol for exchange of information in a


decentralized, distributed environment.
• It is an XML based protocol that consists of three parts:

1. SOAP envelope that defines a framework for describing what is in a message


and how to process it.
2. SOAP encoding rules for expressing instances of application-defined data
types.

3. SOAP for representing remote procedure calls and responses.


• SOAP Envelope
– the primary container
– root element of the message
– using the XML namespace
http://schemas.xmlsoap.org/soap/envelope/ (As per SOAP 1.1
spec.)
• SOAP Header
– first immediate child element of a SOAP envelope
– zero or more optional child elements
– mustUnderstand attribute
• represents a transaction semantics entry
– provide mechanisms to extend
• defining high-level functionalities such as security,
transactions, priority, and auditing
SOAP Header
• The Header element is encoded as the first immediate child element of the
SOAP Envelope XML element. All immediate child elements of the Header
element are called header entries.
• The encoding rules for header entries are as follows:
1. A header entry is identified by its fully qualified element name, which
consists of the namespace URI and the local name.
2. The SOAP encodingStyle attribute MAY be used to indicate the encoding style
used for the header entries.
3. The SOAP mustUnderstand attribute.

<SOAP-ENV:Header>
   <t:Transaction
      xmlns:t="some-URI" SOAP-ENV:mustUnderstand="1">
          5
   </t:Transaction>
</SOAP-ENV:Header>
• SOAP Body
– SOAP envelope’s child element
– one or more optional SOAP body block entries
– mandatory processing information or the payload intended for the
receiver
– Body block can contain…
• RPC method and its parameters
• Target application (receiver) specific data
• SOAP fault for reporting errors and status information
• SOAP Fault
– error and/or status information
– elements to define the error and status
• Faultcode (VersionMismatch, MustUnderstand, Client, Server)
Faultstring, Faultactor, Detail
• SOAP mustUnderstand
– the processing of a SOAP header block is mandatory
or optional at the target SOAP node

• SOAP Attachments
– An XML format and any data format (can be ASCII or
binary)
Examples of SOAP Messages
• SOAP Message Embedded in HTTP Request

POST /StockQuote HTTP/1.1


Host: www.stockquoteserver.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
SOAPAction: "Some-URI"

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
       <m:GetLastTradePrice xmlns:m="Some-URI">
           <symbol>L&T</symbol>
       </m:GetLastTradePrice>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP Message Embedded in HTTP Response

• HTTP/1.1 200 OK
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
<SOAP-ENV:Envelope  xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/">  
<SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>

   <SOAP-ENV:Body>
       <m:GetLastTradePriceResponse xmlns:m="Some-URI">
           <Price>34.5</Price>
       </m:GetLastTradePriceResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP Communication

• SOAP RPC
– remote procedural call-based synchronous
communication
– tightly coupled communication model based on
requests and responses.
• SOAP Messaging
– document-driven communication
– Using synchronous and asynchronous messaging
– loosely coupled communication model based on
message notification and the exchange of XML
documents
Web service Description Language -WSDL
• “Interface Definition Language (IDL) service based on XML that
defines the service interface and its implementation characteristics.”
WSDL document contains following (W3C spec):
 Types– a container for data type definitions using some type system
(such as XSD).
 Message– an abstract, typed definition of the data being communicated.
 Operation– an abstract description of an action supported by the
service.
 Port Type–an abstract set of operations supported by one or more
endpoints.
 Binding– a concrete protocol and data format specification for a
particular port type.
 Port– a single endpoint defined as a combination of a binding and a
network address.
 Service– a collection of related endpoints.
The main structure of a WSDL document :

• <definitions>

<types>
  definition of types........
</types>

<message>
  definition of a message....
</message>

<portType>
  definition of a port.......
</portType>

<binding>
  definition of a binding....
</binding>

</definitions>
 WSDL Ports
The <portType> element is the most important WSDL element.
It describes a web service, the operations that can be performed, and the
messages that are involved.

 WSDL Messages
The <message> element defines the data elements of an operation.
Each message can consist of one or more parts. The parts can be compared
to the parameters of a function call in a traditional programming language.

WSDL Types
The <types> element defines the data types that are used by the web
service.
For maximum platform neutrality, WSDL uses XML Schema syntax to define
data types.

WSDL Bindings
The <binding> element defines the message format and protocol details for
each port.
UDDI
• The Universal Description, Discovery, and Integration (UDDI) specifications
define how to publish and discover information about services in a UDDI-
conforming registry.

• The UDDI schema identifies the types of XML data structures that comprise an
entry in the registry for a service.

• UDDI registry as a "Yellow Pages" for web services.

• Web services uses UDDI for discovery and lookup services.


Basic Web Services

Points to
UDDI description
Registry
WSDL

Points to
service
Finds Describes
Service Service

Web
WebService
Service SOAP Web
WebService
Service
Client
Client (J2EE,
(J2EE,PL/SQL,
PL/SQL,
(J2EE, .NET,C/C++,
(J2EE,.NET,
.NET, .NET,C/C++,
PL/SQL Legacy
Legacy…)
PL/SQL…)…) Invokes with …)
XML Messages
Building Web Services with JAX-WS
What is JAX-WS
JAX-WS is a technology for building web services and
clients that communicate using XML.
 JAX-WS allows developers to write message-
oriented as well as RPC-oriented web services.
 In JAX-WS, a remote procedure call is represented by
an XML-based protocol such as SOAP.
Why JAX-WS ?
Platform independence of the Java programming
language.
JAX-WS client can access a web service that is not
running on the Java platform, and vice versa.
It provides a complete Web services stack that eases
the task of developing and deploying Web services.
JAX-WS also includes the Java Architecture for XML
Binding (JAXB) and SOAP with Attachments API for Java
(SAAJ).
 JAX-WS speeds up Web services development by
providing a library of annotations to turn plain old
Java object (POJO) classes into Web services.

 It also specifies a detailed mapping from a service


defined in the Web Services Description Language
(WSDL) to the Java classes that implement that
service.
JAX-WS supports two approaches

Contract first: Start with a WSDL contract, and


generate a Java class to implement the service.
Code first: Start with a Java class, and use
annotations to generate both a WSDL file and a
Java interface.
Creating a Simple Web Service and Client with JAX-WS
Developing a JAX-WS web service is a Java class annotated with
the javax.jws.WebService annotation.
The @WebService annotation defines the class as a web
service endpoint.

A service endpoint interface (SEI) is a Java interface that


declares the methods that a client can invoke on the service.
An SEI is not required when building a JAX-WS endpoint. The
web service implementation class implicitly defines a SEI.
Use the endpoint implementation class and
The wsgen tool to generate the web service artifacts
and the stubs that connect a web service client to the
JAX-WS runtime.
Basic steps for creating the web service and client:

Code the implementation class.


Compile the implementation class.
Deploy the WAR file. The tie classes (which are used to
communicate with clients) are generated by the Application
Server during deployment.
Code the client class.
Use wsimport to generate and compile the stub files.
Compile the client class.
Run the client.
Thank You

You might also like