62
Web Service YU-RONG CHEN June 5 th 2014

Web Service

Embed Size (px)

DESCRIPTION

Web Service. YU-RONG CHEN June 5 th 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 - PowerPoint PPT Presentation

Citation preview

Web Service

YU-RONG CHENJune 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

Web Services Description Language

Simple Object Access Protocol

Universal Description, Discovery and Integration

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:

• Concrete(implementation) definition

Element Description

<types> Data type definition

<message> operation parameters

<operation> abstract description of service actions

<portType> set of operation definitions

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

Abstract data type definitions

Data Name Type

GetWeather- ComplexType

CityName String

CountryName String

GetWeatherResponse- ComplexType

GetWeatherResult String

GetCityByCountry- ComplexType

CountryName String

GetCityByCountryReponse- ComplexType

GetCityByCountryResult String

Data that is sent

Data that is returned

Message Name Data type

GetWeatherSoapIn GetWeather

GetWeatherSoapOut GetWeatherResponse

GetCityByCountrySoapIn GetCityByCountry

GetCityByCountrySoapOut GetCityByCountryResponse

GetWeatherHttpGetIn CityNameCountryName

GetWeahterHttpGetOut Body

GetCityByCountryHttpGetIn CountryName

GetCityByCountryHttpGetOut Body

GetWeatherHttpPostIn CityNameCountryName

GetWeatherHttpPostOut Body

GetCityByCountryHttpPostIn CountryName

GetCityByCountryHttpPostOut Body

portType with operations

Operations with request & response message

PortType Name Operation Name Description Input/Output(mesage)

GlobalWeatherSoap GetWeather Get weather report for all major cities around the world

GetWeatherSoapIn/GetWeatherSopaOut

GetCityByCountry Get all major cities by country name(full/part)

GetCityByCountrySoapIn/GetCityByCountrySoapOut

GlobalWeatherHttpGet 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

GlobalWeatherHttpPost 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

Bind an abstract operation to this implementation and

map the abstract input and output messages to these concrete messages

binding Name Type(portType) Operation Implementation

GlobalWeatherSoap GlobalWeatherSoap GetWeatherAction:http://www.webserviceX.NET/GetWeatherStyle: documentInput: literalOutput: literalGetCitiesByCountryAction:http://www.webserviceX.NET/GetCitiesByCountryStyle: documentInput: literalOutput: literal

GlobalWeatherSoap12 GlobalWeatherSoap GetWeatherAction:http://www.webserviceX.NET/GetWeatherStyle: documentInput:literalOutput:literalGetCitiesByCountryAction: http://www.webserviceX.NET/GetCitiesByCountryStyle: documentInput: literalOutput:literal

binding Name Type(portType) Operation Impementation

GlobalWeatherHttpGet GlobalWeatherHttpGet

Verb: Get

GetWeatherlocation:/GetWeatherInput: urlEncodedOutput: mime:mimeXml part="Body"GetCitiesByCountryLocation:/GetCitiesByCountryInput: urlEncodedOutput: mime:mimeXml part="Body"

GlobalWeatherHttpPost GlobalWeatherHttpPost

Verb:Post

GetWeatherlocation:/GetWeatherInput: mime:content type="application/x-www-form-urlencoded"/Output: mime:mimeXml part="Body"GetCitiesByCountryLocation:/GetCitiesByCountryInput: mime:content type="application/x-www-form-urlencoded"/Output: mime:mimeXml part="Body"

Service name

Network address of service

Service Name Port Name Binding Location

GlobalWeather

GlobalWeatherSoap GlobalWeatherSoap

http://www.webservicex.net/globalweather.asmx

GlobalWeatherSoap12 GlobalWeatherSoap12

http://www.webservicex.net/globalweather.asmx

GlobalWeatherHttpGet GlobalWeatherHttpGet

http://www.webservicex.net/globalweather.asmx

GlobalWeatherHttpPost GlobalWeatherHttpPost

http://www.webservicex.net/globalweather.asmx

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 cashOutput 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)

Representation(Content Type)

Browser

REST is kind of behavior to transfer the state of content type

REST Triangle(Resources)

HTTP methodsMethod 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/

documentation/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: GETMethod 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