Download Web Service

Document related concepts

URL redirection wikipedia , lookup

Transcript
Web Service
YU-RONG CHEN
June 5th 2014
Outline
• Web Service
–
–
–
–
SOAP
WSDL
UDDI
Implementation
• RESTful Web Service
– REST
– Example
– Implementation
• Comparison
What is a Web Service?
• “a method of communication between two
electronic devices over the Web”
– From Wiki
• “a software system designed to support
interoperable machine-to-machine interaction
over a network”
– From W3C (World Wide Web Consortium)
Why we use web services?
• Interoperability
• Web Services take Web-applications to the
next level
• Web Services have two types of uses
– Reusable application-components
– Connect existing software
Web Service Architecture
Universal Description, Discovery
and Integration
Web Services Description
Language
Simple Object Access Protocol
SOAP
• Simple Object Access Protocol
• An XML based protocol for accessing Web
Services.
• Exchanges messages between different
machines.
– codifies the use of XML as an encoding scheme for
request and response parameters using HTTP as a
means for transport.
`
WSDL
• Web Services Description Language
• WSDL is an XML-based language for describing
– what a service does
– where it resides
– how to invoke it
• Characteristics of WSDL
– Operations and messages are described abstractly
– Defines bindings to message formats and
protocols
WSDL document content
• Abstract(interface) definition:
Element
Description
<types>
Data type definition
<message>
operation parameters
<operation>
abstract description of service actions
<portType>
set of operation definitions
• Concrete(implementation) definition
Element
Description
<binding>
operation bindings
<port>
association of an endpoint with a binding
<service>
location/address for each binding
WSDL Example
http://www.webservicex.net/globalweather.asmx?WSDL
Data Name
Type
GetWeather-
ComplexType
Abstract data type definitions
CityName
String
CountryName
String
GetWeatherResponseGetWeatherResult
GetCityByCountry-
CountryName
GetCityByCountryReponseGetCityByCountryResult
ComplexType
String
ComplexType
String
ComplexType
String
Message Name
Data type
GetWeatherSoapIn
GetWeather
GetWeatherSoapOut
GetCityByCountrySoapIn
GetWeatherResponse
Data that is sent
GetCityByCountry
GetCityByCountrySoapOut
GetCityByCountryResponse
GetWeatherHttpGetIn
CityName
CountryName
GetWeahterHttpGetOut
Body
GetCityByCountryHttpGetIn
CountryName
GetCityByCountryHttpGetOut
Body
GetWeatherHttpPostIn
CityName
CountryName
GetWeatherHttpPostOut
Body
GetCityByCountryHttpPostIn
CountryName
GetCityByCountryHttpPostOut
Body
Data that is returned
portType with operations
PortType Name
Operation Name
Description
GlobalWeatherSoap
GetWeather
Get weather report for all
GetWeatherSoapIn/
Operations with request & response message
major cities around the world
GetWeatherSopaOut
GetCityByCountry
Get all major cities by country
name(full/part)
GetCityByCountrySoapIn/
GetCityByCountrySoapOut
GetWeather
Get weather report for all
major cities around the world
GetWeatherHttpGetIn/
GetWeatherHttpGetOut
GetCitiesByCountry
Get all major cities by country
name(full / part)
GetCitiesByCountryHttpGetIn/
GetCitiesByCountryHttpGetOut
GetWeather
Get weather report for all
major cities around the world
GetWeatherHttpPostIn/
GetWeatherHttpPostOut
GetCitiesByCountry
Get all major cities by country
name(full / part)
GetCitiesByCountryHttpPostIn/
GetCitiesByCountryHttpPostOut
GlobalWeatherHttpGet
GlobalWeatherHttpPost
Input/Output(mesage)
Bind an abstract operation
to this implementation and
binding Name
Type(portType)
Operation Implementation
GlobalWeatherSoap
GlobalWeatherSoap
GetWeather
map the abstract input and
Action:http://www.webserviceX.NET/GetWeather
output messages to these
Style: document
concrete messages
Input: literal
Output: literal
GetCitiesByCountry
Action:http://www.webserviceX.NET/GetCitiesByCountry
Style: document
Input: literal
Output: literal
GlobalWeatherSoap12
GlobalWeatherSoap
GetWeather
Action:http://www.webserviceX.NET/GetWeather
Style: document
Input:literal
Output:literal
GetCitiesByCountry
Action: http://www.webserviceX.NET/GetCitiesByCountry
Style: document
Input: literal
Output:literal
binding Name
Type(portType)
Operation Impementation
GlobalWeatherHttpGet
GlobalWeatherHttpGet
GetWeather
location:/GetWeather
Input: urlEncoded
Output: mime:mimeXml part="Body"
Verb: Get
GetCitiesByCountry
Location:/GetCitiesByCountry
Input: urlEncoded
Output: mime:mimeXml part="Body"
GlobalWeatherHttpPost
GlobalWeatherHttpPost
Verb:Post
GetWeather
location:/GetWeather
Input: mime:content type=
"application/x-www-form-urlencoded"/
Output: mime:mimeXml part="Body"
GetCitiesByCountry
Location:/GetCitiesByCountry
Input: mime:content type=
"application/x-www-form-urlencoded"/
Output: mime:mimeXml part="Body"
Service Name
Port Name
GlobalWeatherSoap
Binding
Location
Service name
http://www.webs
GlobalWeatherSoap
ervicex.net/global
weather.asmx
GlobalWeatherSoap12
http://www.webs
ervicex.net/global
weather.asmx
GlobalWeatherHttpGet
GlobalWeatherHttpGet
http://www.webs
ervicex.net/global
weather.asmx
GlobalWeatherHttpPost
http://www.webs
ervicex.net/global
GlobalWeatherHttpPost
weather.asmx
Network address of service
GlobalWeatherSoap12
GlobalWeather
UDDI
• Universal description, discovery, and integration
• UDDI is a registry standard for Web service
description and discovery together with a registry
facility that supports the WS publishing and
discovery processes.
• UDDI enables a business to:
– describe its business and its services
– discover other businesses that offer desired services
– integrate (interoperate) with these other businesses
UDDI(cont.)
• Conceptually, a UDDI business registration
consists of three inter-related components
– “white pages” (address, contact, and other key
points of contact)
– “yellow pages” classification info. based on
standard industry taxonomies
– “green pages”, the technical capabilities and
information about services
UDDI Data Structure
• The UDDI XML schema defines 4 core types of
information
– businessEntity: a description of the organization that
provides the service.
– businessService: a list of all the Web services offered
by the business entity.
– bindingTemplate: describes the technical aspects of
the service being offered.
– tModel: (“technical model”) is a generic element that
can be used to store technical information on how to
use the service, conditions for use, guarantees, etc.
How to implement Soap WS?
• Two ways to implement:
– Contract-First
• encourages developers to think of the service contract first, in
terms of XML, using XML schema (.xsd) and WSDL
– Contract-Last
• expose an existing service interface as a web service
• many tools and libraries can help expose a Java class/interface as a
web service
• define the contract for this web service as the last step in the
development process by generating it from Java code
• Develop Contract-Last web Service
– JAVA Service Programs Development
– Web Services Exposition Development
– Web Services Client Development
Environment
• JDK
– jdk1.7.0_17
• Eclipse
– Helios 7.1.1
• Tomcat
– apache-tomcat-7.0.37
Programs Development (1/4)
• Create Project
– File  New  Dynamic Web Project
Programs Development (2/4)
• Project Name
– PurchaseOrder
• Target runtime
– Apache Tomcat
Programs Development (3/4)
• Create a Class
– Package ws.com
– Class WebService_purchaseOrder
Programs Development (4/4)
• Method
– purchase_order_billTo
Service functionality:
Input two parameter:
String Op1,int cash
Output a String
Web services Development (1/5)
• Create Web services
• Java File  Web Services  Create Web Services
Web services Development (2/5)
Web services Development (3/5)
Methods
Web services Development (4/5)
Web services Development (5/5)
Generate wsdl
WSDL
WSDL
Test WSDL
Web services Client Development (1/1)
• New a Dynamic Web Project
• New a Web Service Client
Web services Client Development (1/4)
• Choose WSDL
Web services Client Development (2/4)
• Use Test Client
Web services Client Development (3/4)
• Choose output folder
Web services Client Development (4/4)
• Client Test configuration setting
Web services Client Test Page
Criticisms
• Too complex, not open source
• A custom interface requires a custom client for
every service
• Concerns about performance due to XML and
SOAP/HTTP in enveloping and transporting
RESTful Web Service
REST(REpresentational State Transfer)
• Introduced by Roy Fielding in his doctoral
dissertation
– He is one of the principal authors of the HTTP specification
version 1.0 and 1.1
• A software architectural style.
• Interact with stateful resources, rather than
messages or operations
• Use HTTP standard operations such as GET, POST,
PUT, DELETE
Noun(Resource)
Verb(method)
Browser
Representation(Content Type)
REST is kind of behavior to transfer the
state of content type
REST Triangle
(Resources)
HTTP methods
Method
CRUD
Description
Example (Verb+Resource)
GET
Read
Retrieve a current resource
representation
GET http://book.com/books/1
PUT
Update
Modify a resource representation
PUT http://book.com/books/10
POST
Create
Initial a new resource
representation
POST http://book.com/books/100
DELETE
Delete
Clear a resource representation
DELETE http://book.com/books/1
Architectural Elements
• Component
– is an abstract unit of software instructions and
internal state that provides a transformation of data
via its interface
• Connector
– is an abstract mechanism that mediates
communication, coordination, or cooperation among
components
• Data
– is an element of information that is transferred from a
component, or received by a component, via a
connector
Architectural Constraints
• Client-server
• Stateless
– Server contains no client state
– Self-descriptive messages
• Cacheable
– Clients can cache responses
• Layered system
– Clients cannot tell whether it’s connected directly to the end server, or
an intermediary
• Code on demand (optional)
– Servers can temporarily extend or customize the functionality of a
client by the transfer of executable code.
• Uniform interface
REST-Conclusion
• Violating any of required constraint means
service is not strictly RESTful
• Compliance with REST constraints allows:
– Scalability
– Simplicity
– Modifiability
– Visibility
– Portability
– Reliability
RESTful Web Service
• A RESTFul web service is based on HTTP
methods and the concept of REST
• is defined with these aspects:
– base URI
– an Internet media type for the data
– standard HTTP methods(e.g., GET, PUT, POST, or
DELETE)
RESTful WS – Example(1)
• Twitter API
– https://dev.twitter.com/docs/api/1.1
RESTful WS – Example(2)
• Google Map API(Google Distance Matrix)
– https://developers.google.com/maps/documentat
ion/distancematrix/?hl=en
RESTful WS-Implementation(1/7)
• new a dynamic web project
RESTful WS-Implementation(2/7)
• Add jar files to WEB-INF/lib
RESTful WS-Implementation(3/7)
RESTful WS-Implementation(4/7)
• UserInfo.java
Class level path in URI
Verb: GET
Method level path in URI
Content type: XML
Inject the value of URI parameter
Return
<User><Name>i<Name></User>
RESTful WS-Implementation(5/7)
• Add web.xml into WEB-INF
RESTful WS-Implementation(6/7)
http://localhost:8080/RESTfulWS/rest/UserInfoService/name/YURONG
RESTful WS-Implementation(7/7)
• Creating Client - UserInfoClient.java
Comparison