REST 深度解析:超媒体

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

HATEOAS:TheConfusingPartofRESTDr.JimWebberWeb:@jimwebberRoadmap•TheRichardsonMaturityModel•HypermediaFormats•HATEOAS–HypermediaAsTheEngineOfApplicationState•RESTfulbusinessprotocolsTheRichardsonMaturityModel•Level0–SOAP,XMLRPC,POX–SingleURI•Level1–URITunnelling–ManyURIs,Singleverb•Level2–ManyURIs,manyverbs–CRUDservices(e.g.AmazonS3)•Level3–Level2+Hypermedia–RESTfulServicesHypermediaHTTPURIWhytheWeb?WhybeRESTful?•Scalable•Fault-tolerant•Recoverable•Secure•Looselycoupled•Preciselythesamecharacteristicswewantinbusinesssoftwaresystems!MediaTypesRule!•TheWeb’scontractsareexpressedintermsofmediatypesandlinkrelations–Ifyouknowthetype,youcanprocessthecontent•SometypesarespecialbecausetheyworkinharmonywiththeWeb–Wecallthese“hypermediaformats”(Hyper)mediatypesProcessingmodelHypermediacontrols(linksandforms)Supportedoperations(methods,headersandstatuscodes)StandardisedmediatypeRepresentationformats(mayincludeschemas)Composeapplication-specificbehavioursontopofthehandlingofstandardisedmediatypesGeneralSpecificPlainOldXMLisnotHypermediaFriendlyHTTP/1.1200OKContent-Length:227Content-Type:application/xmlDate:Wed,19Nov200821:48:10GMTorderxmlns=’stheprotocol?Sowhat?•Howdoyouknowthenextthingtodo?•Howdoyouknowtheresourcesyou’remeanttointeractwithnext?•Inshort,howdoyouknowtheservice’sprotocol?–TurntoWADL?Yuck!–Readthedocumentation?Comeon!–URITemplates?TightCoupling!URITemplatesareNOTaHypermediaSubstitute•OftenURItemplatesareusedtoadvertiseallresourcesaservicehosts–Dowereallyneedtoadvertisethemall?•Thisisverbose•Thisisout-of-bandcommunication•Thisencouragestight-couplingtoresourcesthroughtheirURItemplate•Thishastheopportunitytocausetrouble!–Knowledgeof“deep”URIsisbakedintoconsumingprograms–Servicesencapsulationisweakandconsumerswillprogramtoit–ServicewillchangeitsimplementationandbreakconsumersBadIdeaswithURITemplates•Imaginewe’recreatedanorder,whatnext?•WecouldsharethisURItemplate:–{order_id}•Theorder_idfieldshouldmatchtheorderIDthatcamefromtherestbucksservice–Soundsgreat!•ButwhatifRestbucksoutsourcespayment?–ChangetheURIforpayments,breakthetemplate,breakconsumers!–D’oh!•Becarefulwhatyoushare!TwoCommonHypermediaFormats:XHTMLandATOM•Botharecommonplacetoday•Botharehypermediaformats–Theycontainlinks•Bothhaveaprocessingmodelthatexplicitlysupportslinks•Whichmeansbothcandescribeprotocols…What’sthebigdealwithXHTML?•Itdoestwointerestingthings:1.Itgivesusdocumentstructure2.Itgivesuslinks•So?1.Wecanunderstandtheformatofthoseresources2.Wecandiscoverotherresources!•How?1.Followthelinks!2.Encodetheresourcerepresentationas“normal”XMLinyourXHTMLdocuments•ContrastthiswiththeAtomandAPPapproach...similar!XHTMLinActionhtmlxmlns==orderpclass=locationtakeAway/pulclass=itemsliclass=itempclass=namelatte/ppclass=quantity1/ppclass=milkwhole/ppclass=sizesmall/p/li/ulahref==paymentpayment/a/div/body/htmlBusinessdata“HypermediaControl”AtomSyndicationFormat•Theapplication/atom+xmlmediatypeishypermediaaware•Youshouldexpectlinkswhenprocessingsuchrepresentations•Andbepreparedtodothingswiththem!HTTP/1.1200OKContent-Length:342Content-Type:application/atom+xmlDate:Sun,22Mar200917:04:10GMTentryxmlns==paymenthref==special-offerhref=://restbucks.com/order/1234/idupdated2009-03-22T16:57:02Z/updatedsummary1xCafeLatte/summary/entryapplication/atom+xml•Therelcarriessemantics•Atomisahypermediaformat–Bothfeedsandentriescontainshypermediacontrolsthatcandescribeprotocols•Andithasreachapplication/vnd.restbucks+xml•Whatamouthful!•Thevndnamespaceisforproprietarymediatypes–AsopposedtotheIANA-registeredones•RestbucksownXMLisahybrid–WeuseplainoldXMLtoconveyinformation–Andlinkelementstoconveyprotocol•Thisisimportant,sinceitallowsustocreateRESTful,hypermediaawareservicesLinks•ConnectednessisgoodinWeb-basedsystems•ResourcerepresentationscancontainotherURIs•Linksactasstatetransitions•Application(conversation)stateiscapturedintermsofthesestatesdrinknamelatte/namemilkwhole/milksizesmall/sizelinkhref===application/vnd.restbucks+xml//drinkAnatomyofalinkInformationprocessingWHATformdoesthelinkedresourcetake?AddressWHEREdoesthelinkedresourcereside?SemanticcontextWHYaccessthelinkedresource?ProjectingContractswithLinks•ThevalueoftheWebisits“linked-ness”–LinksonaWebpageconstituteacontractforpagetraversals•UseLinkstodescribestatetransitionsinprogrammaticWebservices•Hypermediasupportlinks–Allowustodescribehigher-orderprotocolswhichsitcomfortab

1 / 56
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功