You are on page 1of 30

Diana Beleiu Application Developer

2nd of June 2015

Introduction to Web Services

Introduction

What are Web Services?

Web Service Terminology

SOAP Web Services

XML

XML Namespaces

Agenda

XML Schema

WSDL - Web Service Definition Language

SOAP Web Services

JAX-WS

Code First or Contract First?

JAX-WS handlers types

SOA

What are Web Services?


A software concept and infrastructure supported by several major computing
vendors (notably Microsoft and IBM) for program-to-program
communication and application component delivery. The Web services
concept treats software as a set of services accessible over ubiquitous
networks using Web-based standards and protocols. (Gartner)
A Web service is a software system designed to support interoperable
machine-to-machine interaction over a network. It has an interface described
in a machine-processable format (specifically WSDL). Other systems interact
with the Web service in a manner prescribed by its description using SOAPmessages, typically conveyed using HTTP with an XML serialization in
conjunction with other Web-related standards. (W3C)

What are Web Services? (2)

Web services are application components

Web services communicate using open protocols

Web services are self-contained and self-describing

Web services can be discovered using UDDI

Web services can be used by other applications

HTTP and XML is the basis for Web services

Company A

Company B

Web Service Client

Web Service
3. interact

Web Services middleware


(internal)

Web Services middleware


(internal)

Other tiers

Other tiers

2. Find

1. Publish the service description

Service descriptions

Company C (directory service provider)

What are Web Services? (3)


There are 2 categories of web services:
Big web services (SOAP web services)
Restful web services (REST web services)
SOAP web services:
Oldest and quite used today;
Uses SOAP protocol over HTTP (or SMTP, JMS, etc);
Are described by WSDL file;
Can exchange SOAP messages;
REST web services:
newer and easier than SOAP based web services;
uses HTTP as communication protocol;
used for exchanging representation of resources;
can exchange XML or JSON messages;

Web Service Terminology

SOAP
(Simple Object Access Protocol)

XML
(Extensible Markup Language)

WSDL
(Web Service Definition Language)

XSD
(XML Schema Definition)

SEI
(Service Endpoint Interface)

SOA
(Service Oriented Architecture)

May 14, 2015

IBM Confidential

SOAP Web Services


SOAP, originally defined as Simple Object Access Protocol, is a protocol
specification for exchanging structured information in the implementation of
Web Services in computer networks.
it relies on Extensible Markup Language (XML) as its message format, and
usually relies on other Application Layer protocols (most notably
Remote Procedure Call - RPC and HTTP) for message negotiation and
transmission
SOAP can form the foundation layer of a web services protocol stack, providing
a basic messaging framework upon which web services can be built.

XML
XML stands for EXtensible Markup Language.
XML was designed to describe data.
XML is a software and hardware independent tool for carrying information.

The advantages of using XML:

Easy to understand and read

Supported by a large number of platforms

Used across open standards that are available today

Developers can create their own data definitions and models of representation

Simpler to use than binary formats to represent complex data structures

XML Namespaces
Similar to Java packages:
Are used to avoid name collision;
Facilitate grouping of elements;

<html:table
xmlns:html="http://www.w3.org/TR/html
4/">
<html:tr>

To know which element belongs to which group;

<html:td>Bruxelles</html:td>

Can be used as a version control scheme

<html:td>Frankfurt am Main/html:td>

The namespace is changed when a new version


of the application is released
Sintax of namespace declaration:
xmlns:prefix=URI
xmlns is reserved keyword;
prefix is user defined;
URI identifies the namespace;
When the prefix is omitted, the declaration
define a default namespace

</html:tr>
</html:table>
<jobs:table
xmlns:jobs="http://www.ibm.com/erpb">
<jobs:name>Java Developer</jobs:name>
<jobs:years>Network admin</jobs:years>
</jobs:table>

XML Schema (XSD)


= describes the structure behind an XML
(just as classes are for Objects)
XML tags,
e.g. employee
(through XSD <element>)
their contents
e.g. <address> contains a <city>
(through XSD types)
and attributes

<employee id="13512512">
<name>John Doe</name>
<phone>0720654321</phone>
<phone>0720123456</phone>
<cnp>1210186410071</cnp>
<birthDate>1986-10-21</birthDate>
<address>
<city>Bucuresti</city>
<street>Viorele</street>
<streetNumber>4</streetNumber>
</address>
</employee>

XML Schema
<employee id="13512512">
<name>John Doe</name>
<phone>0720654321</phone>
<schema
<phone>0720123456</phone>
xmlns="http://www.w3.org/2001/XMLSchema"
<cnp>1210186410071</cnp>
TargetNamespace="..."
<birthDate>1986-10-21</birthDate>
xmlns:xs="http://www.w3.org/2001/XMLSchema"
<address>
xmlns:tns="http://www.example.org/employee"
<city>Bucuresti</city>
xmlns:a="http://www.example.org/address">
<street>Viorele</street>
<element name="employee" type="tns:Employee"/> <streetNumber>4</streetNumber>
</address>
</employee>
<complexType name="Employee">
<sequence>
<element name="name" type="xs:string"/>
<element name="phone" maxOccurs="10" type="xs:string"/>
<element name="birthDate" type="xs:date"/>
<element name="cnp" type="xs:long"/>
<element name="address" type="a:Address"/>
</sequence>
</complexType>

DEMO!

...
</schema>

SOAP Web Services (2)


UDDI is a platformindependent, XML-based
registry by which businesses
worldwide can list themselves
on the Internet, and a
mechanism to register and
locate web service
applications
The WSDL is an XML file
which describes what methods
are available for the web
service and what parameters
you need to include
The service
requester is a
client that is
using the web
service

13

May 14, 2015

IBM Confidential

SOAP is the way the client and


the web service communicate

The service provider is a


computer running server
software (Apache,
Glassfish etc.) where the
web service is deployed

WSDL - Web Service Definition Language


=XML language for describing remote services
WSDL describes:
Remote operations signature

Name

parameters

return type(s)

How to access the remote service

How to serialize parameters

The protocol to communicate


with remote server

Address of the remote service

JAX-WS
The Java API for XML-based Web services (JSR-224), commonly abbreviated
JAX-WS, is the next generation Web services programming model.
It replaces the earlier JAX-RPC technology.
JAX-WS defines a model that uses Java annotations to develop Web service
providers and Web service clients
Java EE 5 platform requires compatible application servers to support JAX-WS
2.0, WebSphere Application Server V7 has already taken the next step and
supports JAX-WS 2.1

DEMO!

JAX-WS (2)
JAX-WS, like all specifications, is divided into 2 parts
The First part consists of an API provided by SUN.
This API is accompanied by a JSR.
The API consists of a set of interfaces, annotations, xml files and abstract
classes (sometimes maybe a few base classes) which are to be used when
using that feature.
One very important thing about this API that should be noted is that the API in
itself is not capable of achieving the intended objective of the specification.
The JSR lays down a set of rules and regulations governing the API.
The Second part of any specification is the implementation.
The implementation follows all the rules laid down by the JSR and uses the API
(i.e. provides all implementation for interfaces, and classes that use the
annotations to get the desired objective) to provide a base.
Multiple reference implementations by different vendors like: Sun (JDK 1.6),
Apache (CXF, AXIS 2), IBM (Web sphere), ORACLE (Weblogic App Server),
Glassfish (Metro).

SOAP I am a Java developer. Do I need to know


SOAP?
Yes
Understanding it will help you to build
better applications;
No
You will mostly use high level API
(JAX-WS, JAX-RPC, .NET) to
build web services;
How SOAP works is hidden from
developers;

<?xml version="1.0" encoding="UTF-8"?>


<soap:Envelope xmlns:soap="
http://www.w3.org/2003/05/soap-envelope"
xmlns:a=http://www.ibm.com/ws/Authentificator
>
<soap:Header>
<!-- Header blocks go here -->
</soap:Header>
<soap:Body>
<a:authentificate xmlns:a=
http://www.ibm.com/ws/Authentificator>
<a:username>Ionescu</a:username>
<a:password>parola</a:password>
</a:authentificate>
</soap:Body>
</soap:Envelope>

Web Service Implementation


The simplest way to create a web service consists of the following steps:
1. Create an implementation class
2. Annotate the class with @WebService annotation
3. Annotate the methods to be exposed with @WebMethod annotation
4. Publish the web service via Endpoint.publish
5. Get WSDL
6.Test service using Eclipse Web Service Explorer / SoapUI
7. Deploy JAX-WS web services on Tomcat
Test with SOAP UI

DEMO!

Code First or Contract First?


In the Code First approach, development of the service is started from a code.
So the developer can write a service without knowing anything about WSDL.

Contract-first is the generally accepted 'best practice.'


When using contract-first, you start with the WSDL contract, and use Java to
implement said contract.

What is WSDL?
WSDL stands for Web Services Description Language.
A WSDL file is an XML document that describes a Web
service.It specifies the location of the service and the
operations (or methods) the service exposes.

Contract First - WSDL


<definitions name="AuthentificatorService" xmlns="http://schemas.xmlsoap.org/wsdl/" >
<types>
<xsd:schema>
<xsd:import namespace="http://www.ibm.com/ws/Authentificator"
schemaLocation="token.xsd" />
</xsd:schema>
</types>
<message name="tokenRequest">
<part name="username" element="tns:username" />
<part name="password" element="tns:password" />
</message>
<message name="tokenResponse">
<part name="result" element="tns:tokenResponse" />
</message>
<portType name="AuthentificatorPort">
<operation name="authentificate">
<input message="tns:tokenRequest" />
<output message="tns:tokenResponse" />
</operation>
</portType>
<binding name="AuthentificatorPortBinding" type="tns:AuthentificatorPort">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name="authentificate">
<soap:operation soapAction="urn:authentificate" />
<input> <soap:body use="literal" /> </input>
<output> <soap:body use="literal" /> </output>
</operation>
</binding>
<service name="AuthentificatorService">
<port name="AuthentificatorPort" binding="tns:AuthentificatorPortBinding">
<soap:address location="http://www.ibm.com/ws/Authentificator" />
</port>
</service>
</definitions>

Code First or Contract First? (2)


Contract-First vs Code-First Advantages

Its independent from your programming language

It is more descriptive than a generated WSDL file

youre able to apply more restrictions and rules here that you otherwise
needed to implement in some method in your concrete programming
language
If the server and the client part are implemented by different teams, both
sides are able to begin implementing quickly

DEMO!

JAX-WS handlers types


SOAP Handlers (or, more precisely, protocol handlers, as JAX-WS supports
other non-SOAP protocols such as XML over HTTP), which provide the ability
to modify the entire SOAP envelope using the DOM-like SAAJ (SOAP with
Attachments API for Java).

LogicalHandlers with provide access to the SOAP body's content (the payload)
only. For logical handlers, JAXB or one of the Source options is used to
modify the message instead of the protocol-specific SAAJ.
Handlers carry a MessageContext - a thread safe mechanism holding
request/response data between the handlers of a chain, and between the client
or service endpoints and the handlers

DEMO!

JAX-WS handlers types (2)

Service-Oriented Architecture
A service-oriented architecture (SOA) is an architectural pattern in computer
software design in which application components provide services to other
components via a communications protocol, typically over a network.

The principles of service-orientation are independent of any vendor, product or


technology.

SOA and Web Services

SOA and Web Services (2)


Strengths

Weaknesses

Low follow-up costs


Very flexible architecture and
governance
Compliance with standards
Supported by all major software houses

Opportunities

Threats

Individual systems can be implemented


processes and orchestrated easily

Lack of focus on relevant business

High startup and infrastructure costs


Requires a comprehensive SOA strategy

Stages of web services developer


1. Denial - It's Simple Object Access Protocol, right?
2. Over Involvement - OK, I'll read the SOAP, WSDL, WS-I BP, JAX-RPC, SAAJ,
JAX-P... specs. next, I'll check the Wiki and finally follow an example showing
service and client sides.
3. Anger - I can't believe those #$%&*@s made it so difficult!
4. Guilt - Everyone is using Web Services, it must be me, I must be missing
something.
5. Acceptance - It is what it is, Web Services aren't simple or easy.
(Mark Hansen Dave Podnar's Five Stages of dealing with web services)

27

Links
Web Services Description Language (WSDL) 1.1
http://www.w3.org/TR/wsdl
SOAP 1.2 specifications
http://www.w3.org/TR/soap/
JAXB 2.0 specifications
http://jcp.org/aboutJava/communityprocess/mrel/jsr222/index.html
JAX-RPC
http://jcp.org/en/jsr/detail?id=101
JAX-WS 2.0
http://jcp.org/en/jsr/detail?id=224
Web services metadata
http://jcp.org/en/jsr/detail?id=181
JAX-RS
http://jcp.org/en/jsr/detail?id=311

28

Links (2)

IBM JAX-WS presentation


http://publib.boulder.ibm.com/infocenter/ieduasst/v1r1m0/index.jsp?topic=/
com.ibm.iea.wasfpws_v6/wasfpws/6.1/JAX-WS/WASv61_WSFP_JAX-WS_Overview/
er.html
Jboss JAX-WS User Guide
https://docs.jboss.org/author/display/AS71/JAX-WS+User+Guide
Apache web services projects
http://ws.apache.org/
http://cxf.apache.org/
A bunch of free SOAP web services on the net
http://www.webservicex.net/ws/default.aspx
E-bay - documentation for their webservices
https://www.x.com/developers/ebay/documentation-tools/
E-bay API for product search
http://developer.ebay.com/Devzone/finding/Concepts/MakingACall.html
29

Questions?

30

You might also like