You are on page 1of 35

Web Services

1
Agenda
• Introduction to web services
• What is Web Service?
• Why web service?
• WS Components
• How to use?
• Types of Web services
• Advantages –Disadvantages
• SOAP style Hello World web service
• REST style Hello World web service development
• SOAP vs REST
2
Introduction to Web Services
• Web service is distributed technology to achieve
interoperability (portability) across different technologies
such as Java , .NET, Mainframes, PHP, C++, etc

• Web services are self-contained, modular, distributed,


dynamic applications that can be described, published,
located, or invoked over the network to create products,
processes, and supply chains

• These applications can be local, distributed, or web-based

3
What is Web Services
• Available over the Internet or private
(intranet) networks
• Uses a standardized XML messaging system
• Is not tied to any one operating system or
programming language
• Is self-describing via a common XML grammar
• Is discoverable via a simple find mechanism

4
Why web services
• Interoperability
• Exposing the Existing Function on the Network
• Standardized Protocol
• Low CostCommunication

5
Web Services Components
• Following are the WS components
– WSDL (Web Services Description Language)
– SOAP (Simple Object Access Protocol)
– UDDI (Universal Description, Discovery and
Integration)

6
WSDL
• WSDL is an XML-based language for describing web
services and how to access them
• WSDL was developed jointly by Microsoft and IBM
• WSDL is the standard format for describing a web
service.
• WSDL definition describes how to access a web
service and what operations it will perform
• It describes how to interface with XML-based
services
• It is the language that UDDI uses.
7
SOAP
• SOAP is an XML-based protocol for exchanging
information between computers.
• It is a communication protocol
• It is format for sending messages
• Designed to communicate via internet
• It is platform and language independent
• It is simple and extensible

8
UDDI
• UDDI is an XML-based standard for describing,
publishing, and finding web services.
• It is a specification for a distributed registry of web
services
• It is platform independent, open framework
• Uses WSDL to describe interfaces to web services
• UDDI is an open industry initiative enabling
businesses to discover each other and define how
they interact over the Internet

9
Types Of Web Services
• Web services can be designed in 2 different
style
– SOAP
– REST

10
Web Services -SOAP
• Architecture

11
Web Services –SOAP (Cont..)
• Service Provider:
– The service provider creates Web Service.
– Typically the service provider exposes certain business
functioanlity in their organization as a web service.
– The service provider describes service in technology
neutral format (such as wsdl format) and publish in central
repository for wider audience

12
Web Services –SOAP (Cont..)
• Service Registry
– A service registry is a central location where the service
provider can list its web services, and where a service
consumer can search for Web Services.
– The Web Service in a central registry is publicly available to
everyone.
• Service Requester
– Discover service details from central registry and invokes
service

13
How to Use/Access
• There are two ways to access web service
– If Service provider knows client:
• If service provider knows its client then it will provide
its wsdl to client and client will be able to access web
service.
– Service provider register its WSDL to UDDI and
client can access it from UDDI
• Web services can register with a UDDI and make
themselves available through it for discovery.

14
WS Advantages
• Cross platform application communication over network

• Usability- Web Services allow the business logic of many


different systems to be exposed over the Web.

• Reusability-It is easy to reuse Web Service components as


appropriate in other services.

• Deployability-Web Services are deployed over standard


Internet technologies. This makes it possible to deploy Web
Services even over the fire wall to servers running on the
Internet on the other side of the globe 15
WS Disadvantages
• Web service requests are larger than requests encoded with a
binary protocol. The extra size is really only an issue over low-
speed connections, or over extremely busy connections.

• Although HTTP and HTTPS (the core Web protocols) are


simple, they weren't really meant for long-term sessions.

• If a server doesn't receive a request from a client after a


predetermined amount of time, it assumes that the client is
inactive and removes any client information it was keeping.
This extra overhead means more work for Web service
developers.
16
SOAP Style Web Services
• SOAP style web services can be designed using
JAX-WS API
• The are two ways to write JAX-WS application
code:
– RPC style
– Document style.

17
JAX-WS Example RPC Style
• JAX-WS API is inbuilt in JDK, so you don't need to load any
extra jar file for it
• Simply create a new java project in eclipse using File > New >
Java Project.
• Create HelloWorldServer interface which
has sayHello() method which takes name parameter.
• Create HelloWorldServerImpl which
implements HelloWorldServer and its method sayHello(String
name).
• Create HelloWorldServerPublisher which will publish web
service.
• Create HelloWorldClient to consume hello world web service.
18
JAX-WS Example RPC Style (Cont..)

19
JAX-WS Example RPC Style (Cont..)

20
JAX-WS Example RPC Style (Cont..)

21
JAX-WS Example RPC Style (Cont..)
HelloWorldClient.java
package com.demo;
import java.net.URL;
impart javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class HeIIoWorIdCIient{


public static void main(String[] args) {

URL url = new URL("http://localhost:9876/hw?wsdI");


QName qname = new QName("http://javademo.com/", "HelloWorldServerlmplService");
Service service = Service.create(url, qname);
HelloWorldServer server = service.getPort(HeIIoWorIdServer.class);
String name = “John";
System.out.printIn(server.sayHeIIo(name));
) catch (Exception e) {
e.printStackTrace();)} ) 22
REST (Representational State Transfer)
• REST is an architectural style which is based on web-
standards and the HTTP protocol
• REST was first described by Roy Fielding in 2000

• The REST stands for REpresentational State Transfer.


– State means data
– REpresentational means formats (such as xml, json, html,
etc)
– Transfer means carry data between consumer and
provider using HTTP protocol

23
What is REST
• REST is set of guidelines which talks about how client
should interact with server
• As per REST guidelines
– Data and functionality which client can access on server is
known as Resource
– Each resource must be accessed by using it’s URI , by all
the clients
• In a REST based architecture you typically have a
REST server which provides access to the resources
and a REST client which accesses and modifies the
REST resources.
24
HTTP Methods
• Following are the typical HTTP methods used in REST based
architecture.
• GET
– Defines a reading access of the resource without side-effects. The
resource is never changed via a GET request, e.g., the request has no
side effects (idempotent).
• PUT
– Updates a resource. It must also be idempotent.
• DELETE
– removes the resources. The operations are idempotent. They can get
repeated without leading to different results.
• POST
– Creates a new resource
25
RESTful WEB Services
• A RESTFul web services are based on HTTP methods
and the concept of REST.

• A RESTFul web service typically defines the base URI


for the services, the supported MIME-types (XML,
text, JSON, user-defined, …) and the set of
operations (POST, GET, PUT, DELETE) which are
supported.

26
JAX-RS with Jersey
• JAX-RS
– The Java API for RESTful Web Services
• Jersey
– Is the reference implementation for JAX-RS
– The Jersey implementation provides a library to
implement Restful web services in a Java servlet
container.

27
Jersey
• On the server side Jersey provides a servlet
implementation which scans predefined classes to
identify RESTful resources.

• In your web.xml configuration file you register this


servlet for your web application

• The Jersey implementation also provides a client


library to communicate with a RESTful webservice.

28
Installing Jersey
• Manual setup of Jersey libraries in an Eclipse project

• Download the Jersey distribution as zip file from


https://jersey.java.net/download.html

• The zip contains the Jersey implementation JAR and its core
dependencies. It does not provide dependencies for third
party JARs beyond those for JSON support and JavaDoc.

• Copy all JARs from your Jersey download into the


WEB-INF/lib folder.

29
Creating REST Web service
• Create new dynamic web project
• Add jersey jar files
• Create java class
• Define Jersey Servlet dispatcher
• Run your rest service

30
Example (Cont..)
Create java (resource) class

32
Example (Cont..)

33
Example (Cont..)
Define Jersey Servlet dispatcher
You need to register Jersey as the servlet dispatcher for REST request.

34
Example (Cont..)
• Run your rest service:
– Run you web application
– You should be able to access your resources under the
following URL:
– http://localhost:8080/SampleRESTfulWebService/rest/hello

35
SOAP Vs REST
SOAP REST
SOAP uses services interfaces to expose REST uses URI to expose business logic.
the business logic.
JAX-WS is the java API for SOAP web JAX-RS is the java API for RESTful web
services. services.
SOAP defines standards to be strictly REST does not define too much
followed. standards like SOAP.
SOAP requires more bandwidth and REST requires less bandwidth and
resource than REST. resource than SOAP.

SOAP defines its own security. RESTful web services inherits security
measures from the underlying transport

SOAP allows XML data format only. REST allows different data format such
as Plain text, HTML, XML, JSON etc.
36

You might also like