24
Web Services Web Services The SOAP The SOAP & & The REST The REST Edison Lascano

Soap and Rest

Embed Size (px)

Citation preview

Page 1: Soap and Rest

Web ServicesWeb Services

The SOAPThe SOAP&&

The RESTThe REST

Edison Lascano

Page 2: Soap and Rest

Web ServicesWeb ServicesWeb Services: Method of communication for Web Services: Method of communication for web applications (Internet, intranet, extranet, web applications (Internet, intranet, extranet, desktop, mobile) .desktop, mobile) .

Application layer : Application layer :

Mainly the HTTP protocol and port 80.Mainly the HTTP protocol and port 80.

Port and protocol may change, e.g. Port and protocol may change, e.g. SMTPSMTP

Message Formats:Message Formats:

XML, JSON, RSS, longer than binary. XML, JSON, RSS, longer than binary. MIME types match resource types match resource

representations.representations.

Page 3: Soap and Rest

JSON / XMLJSON / XML{"Table": {"httpStatus":"200","primary_key": "val1","key1": {"Table": {"httpStatus":"200","primary_key": "val1","key1":

"value1","key2": "value2"}}"value1","key2": "value2"}}

<tables><tables> <httpStatus value="200"/><httpStatus value="200"/> <table primary_key="val1" key1="value1" key2="value2" /><table primary_key="val1" key1="value1" key2="value2" /> </tables></tables>

{"Student": {"httpStatus":"200","student_id": "005802615","name": {"Student": {"httpStatus":"200","student_id": "005802615","name": "Edison Lascano","address": "2199 McLaughlin Ave"}} "Edison Lascano","address": "2199 McLaughlin Ave"}}

<students><students> <httpStatus value="200"/><httpStatus value="200"/> <student student_id="005802615" name="Edison Lascano" <student student_id="005802615" name="Edison Lascano"

address="McLaughlin" />address="McLaughlin" /> </students></students>

Page 4: Soap and Rest

Web ServicesWeb Services

• The basis of web 2.0 and many distributed The basis of web 2.0 and many distributed applications currentlyapplications currently

• Public APIs are published by web 2.0 Public APIs are published by web 2.0 providers in SOAP, REST, XML-RPC, js. providers in SOAP, REST, XML-RPC, js. Private APIs are used internally.Private APIs are used internally.REST REST http://weather.yahooapis.com/forecastrss?w=2502265

SOAP SOAP http://www.programmableweb.com/api/bing

• MASHUPS (main users of Web APIs)MASHUPS (main users of Web APIs)– Public APIs + local APIs + My ApplicationPublic APIs + local APIs + My Application– Don't develop what is done, use others' Don't develop what is done, use others'

resources. Low coupling. But, APIs may resources. Low coupling. But, APIs may disappear at any moment, disappear at any moment, Yahoo search..

Page 5: Soap and Rest

API´sAPI´s

Page 6: Soap and Rest

TechnologyTechnology

Page 7: Soap and Rest

Web Services in Web 2.0Web Services in Web 2.0• http://www.programmableweb.com . .

Cloud Services providers publish their Cloud Services providers publish their Web APIs: AWS, Google App Engine...Web APIs: AWS, Google App Engine...

• Technology:Technology:

--Tools, Languages, Platforms: Java, .Net, Tools, Languages, Platforms: Java, .Net, Python, php, Ruby, …Python, php, Ruby, …

-Distributed Objects: JEE-EJBs, .Net Remoting; -Distributed Objects: JEE-EJBs, .Net Remoting; ORMs: Persistent Units, JPAs: Linq, ORMs: Persistent Units, JPAs: Linq, Hibernate, Eclipse LinkHibernate, Eclipse Link

-Clients: desktop, gadgets, widgets, mobiles, -Clients: desktop, gadgets, widgets, mobiles, web, Ajax, Flex, JavaFX, Spring... Client-web, Ajax, Flex, JavaFX, Spring... Client-cachecache

Page 8: Soap and Rest

SOAP Web ServicesSOAP Web Services

• Simple Object Access Protocol, Simple Object Access Protocol, acronym dropped after 2003, it is a acronym dropped after 2003, it is a service oriented protocolservice oriented protocol

• Microsoft begins in 1998, currently Microsoft begins in 1998, currently supported by the World Wide supported by the World Wide Consortium W3CConsortium W3C

• Usually Service verb form: URLs Usually Service verb form: URLs Uniform Resource Locator Uniform Resource Locator “getItem”“getItem”

Page 9: Soap and Rest

SOAP Web SOAP Web ServicesServices

• SOAP: protocolSOAP: protocol– SOAP envelope: SOAP Header+SOAP BodySOAP envelope: SOAP Header+SOAP Body– It uses XMLIt uses XML

• WSDL: IDLWSDL: IDL– Description of the methods, parameters, typesDescription of the methods, parameters, types– XMLXML

• UDDI: Universal Description Discovery UDDI: Universal Description Discovery and Integration and Integration – The service brokerThe service broker– Besides the Web ServerBesides the Web Server

ServiceRegistry

ServiceProvider

ServiceConsumer

WSD

L

WS

DL

SOAP Messages

UDDI

Page 10: Soap and Rest

What is a SOAP Web What is a SOAP Web Service?Service?

• Distributed Objects?Distributed Objects?

• Is it RMI?Is it RMI?

• Is it RPC?Is it RPC?

– Every Web Service has a set of Every Web Service has a set of operations,operations,

– Two libraries in Java to program ws Two libraries in Java to program ws are JAX-WS and JAX-RPC.are JAX-WS and JAX-RPC.

Page 11: Soap and Rest

RESTRESTRepresentation State Representation State

TransferTransfer• Architectural styleArchitectural style

• Resource OrientedResource Oriented

• Spread from 2000, It looks like they Spread from 2000, It looks like they always existed, but became to be always existed, but became to be noticed and well organized/architected noticed and well organized/architected after Roy Fielding Dissertation. after Roy Fielding Dissertation. http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

• Fielding is one of the authors of HTTP.Fielding is one of the authors of HTTP.

Page 12: Soap and Rest

RESTREST• A representation of the resource is A representation of the resource is

returnedreturned– Resource: Noun form: URI, Uniform Resource: Noun form: URI, Uniform

Resource IdentifierResource Identifier

• HTTP Methods: GET, PUT, POST, HTTP Methods: GET, PUT, POST, DELETEDELETE

• WADL: Web Application Def LangWADL: Web Application Def Lang– Unlike Soap, WADL is not mandatory to Unlike Soap, WADL is not mandatory to

discover the resource, the providers supply discover the resource, the providers supply development documentation: URI´s lists.development documentation: URI´s lists.

Page 13: Soap and Rest

URIs / URLsURIs / URLs

http://ajax.googleapis.com/ajax/services/language/translate?q=be%20happy?&v=1.0&langpair=en |ko|ko

http://www.youtube.com/embed/u1zgFlCw8Aw?autoplay=1&origin=http://example.com

http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Quito&results=2

http://weather.yahooapis.com/forecastrss?w=2502265

http://developer.yahoo.com/weather/

https://www.dropbox.com/developers/start

https://api.dropbox.com/1/oauth/request_token

Page 14: Soap and Rest

RESTful Web servicesRESTful Web services““If an application complies with the 6 REST If an application complies with the 6 REST constraints, then it is RESTful” Roy Fieldingconstraints, then it is RESTful” Roy Fielding

• Three Client Constraints: Three Client Constraints: • C/S, for separation of concernsC/S, for separation of concerns• Stateless, Client holds the session stateStateless, Client holds the session state• Cacheable,To improve scalability and Cacheable,To improve scalability and

performance, but be careful of getting outdated performance, but be careful of getting outdated informationinformation

• Three Coding Constraints:Three Coding Constraints:• Layered Systems, they do not know about Layered Systems, they do not know about

each other implementations, the URI is enougheach other implementations, the URI is enough• Code on Demand, Javascript, any or “none”Code on Demand, Javascript, any or “none”• Uniform Interface, for all WSs.Uniform Interface, for all WSs.

Page 15: Soap and Rest

RESTful Uniform InterfaceRESTful Uniform Interface• HTTP Request/Response Codes:HTTP Request/Response Codes:

• 200 OK; 300 Redirect; 400 Error, e.g. 404200 OK; 300 Redirect; 400 Error, e.g. 404

• Noun form URIsNoun form URIs. http://www.example.com/resources/. http://www.example.com/resources/

. http://www.example.com/resources/item1 . http://www.example.com/resources/item1

. http://www.example.com/resources/item1/json. http://www.example.com/resources/item1/json

. http://www.example.com/resources/item1?format=json. http://www.example.com/resources/item1?format=json

• HTTP MethodsHTTP Methods• GET GET /items/{id} # read an Item/items/{id} # read an Item• POST POST /items/{id} # create an Item/items/{id} # create an Item• PUT PUT /items/{id} # update an Item/items/{id} # update an Item• DELETE DELETE /items/{id} # delete an Item/items/{id} # delete an Item

Page 16: Soap and Rest

Some REST adviceSome REST advice• Use the standard HTTP methods, not Use the standard HTTP methods, not

as in SOAP, that you must create new as in SOAP, that you must create new methods for every requirement.methods for every requirement.

• Opportunities Opportunities Improve your REST Improve your REST designdesign– Provide one URI for every resourceProvide one URI for every resource– Minimize the use of QUERY_STRING, Minimize the use of QUERY_STRING,

prefer prefer items/1items/1 over over items?id=1items?id=1– Logical over physical URIs, Logical over physical URIs, items/1items/1 over over

items/1.htmlitems/1.html– No verbs in the URI, they are resourcesNo verbs in the URI, they are resources

Page 17: Soap and Rest

More REST adviceMore REST advice

• Use hyperlinks in the Response when need Use hyperlinks in the Response when need to communicate an executed action ack/nack.to communicate an executed action ack/nack.

• Use / to represent parent/child relationships. Use / to represent parent/child relationships. e.g. stock/items1 doctor1/patients/1e.g. stock/items1 doctor1/patients/1

• Use GET to allow the client retrieving a Use GET to allow the client retrieving a resource representation, not POSTresource representation, not POST

• Use POST to update a big object, so the URI Use POST to update a big object, so the URI will not be long and your data can not be will not be long and your data can not be seen in the URI.seen in the URI.

• Use GET, PUT, POST, DELETE properly.Use GET, PUT, POST, DELETE properly.

Page 18: Soap and Rest

The REST way of The REST way of implementing the Web implementing the Web

ServicesServices

Response(HTML/XML/JSON...)

We

b S

erv

er

HTTP GET request URI1

HTTP response

Response(HTML/XML/JSON...)

HTTP GET request URI

HTTP response

HTTP POST URI

HTTP responseURL to submitted PO

PO(HTML/XML/JSON...)

Items List

Item

PO

Adapted from: Roger L. Costello, “REST (Representational State Transfer), XML Technology Course”

Page 19: Soap and Rest

Web app architecture Web app architecture designsdesigns

• Client ↔ REST ↔ EJB ↔ DB (EIS)Client ↔ REST ↔ EJB ↔ DB (EIS)• Client ↔ SOAP ↔ EJB ↔ DB (EIS) Client ↔ SOAP ↔ EJB ↔ DB (EIS)

Client ↔ REST ↔ JPA ↔ DB (EIS) Client ↔ REST ↔ JPA ↔ DB (EIS) Client ↔ REST ↔ DB (EIS), SOAP can be Client ↔ REST ↔ DB (EIS), SOAP can be interchanged with REST in any caseinterchanged with REST in any case

• Client ↔ REST ↔ .NET ↔ ORM ↔ DB(EIS) Client ↔ REST ↔ .NET ↔ ORM ↔ DB(EIS) Client ↔ SOAP ↔ .NET ↔ ORM ↔ DB(EIS) Client ↔ SOAP ↔ .NET ↔ ORM ↔ DB(EIS) .NET can be replaced by any other .NET can be replaced by any other Distributed Objects technology.Distributed Objects technology.

Page 20: Soap and Rest

SOAP web service labSOAP web service lab• Netbeans 7.2 full versionNetbeans 7.2 full version

• Create a new Web Application, then a Create a new Web Application, then a create a new “Web service”create a new “Web service”

• Implement a new operation using the Implement a new operation using the wizards, finally test the Web Service wizards, finally test the Web Service and take a look of the generated WSDL and take a look of the generated WSDL file.file.

Page 21: Soap and Rest

REST Web Service LabREST Web Service Lab• Create a new Web ApplicationCreate a new Web Application

• Create new RESTful web servicesCreate new RESTful web services– From patterns, simple root resourceFrom patterns, simple root resource– New RESTful web services from database, New RESTful web services from database,

finally test the RESTful web services, read finally test the RESTful web services, read the WADL file and then try some URIs to the WADL file and then try some URIs to read the XML/JSON data returned.read the XML/JSON data returned.

Page 22: Soap and Rest

Web Services (Java)Web Services (Java)

SOAPSOAP

JAX-WS JAX-WS annotations:annotations:

@WebService@WebService

@WebMethod@WebMethod

@WebParam@WebParam

RESTREST

JAX-RS JAX-RS annotations:annotations:

@Path@Path

@GET, @PUT, …@GET, @PUT, …

@PathParam, @PathParam, @QueryParam @QueryParam

Page 23: Soap and Rest

Web Services clientsWeb Services clients

SOAPSOAP

JAX-WS stub JAX-WS stub generation from generation from WSDL importWSDL import

RESTREST

Consume through Consume through simple HTTP simple HTTP calls from Java, jscalls from Java, js

WADL is not WADL is not neededneeded

Page 24: Soap and Rest

Motivation for RESTMotivation for REST

"The motivation for developing REST was to create an architectural model forhow the Web should work, such that it could serve as the guiding frameworkfor the Web protocol standards.

REST has been applied to describe the desired Web architecture, help identify existing problems, compare alternative solutions, and ensure that protocol extensions would not violate the core constraints that make the Web successful."

- Roy Fielding