bandwidthisparticularlyimportantandneedstobelimited.restisparticularlyusefulforlimited-profiledevicessuchaspdasandmobilephones,forwhichtheoverheadofheadersandadditionallayersofsoapelementsonthexmlpayloadmustberestricted.
webservicedeliveryoraggregationintoexistingwebsitescanbeenabledeasilywitharestfulstyle.developerscanusetechnologiessuchasasynchronousjavascriptwithxml(ajax)andtoolkitssuchasdirectwebremoting(dwr)toconsumetheservicesintheirwebapplications.ratherthanstartingfromscratch,servicescanbeexposedwithxmlandconsumedbyhtmlpageswithoutsignificantlyrefactoringtheexistingwebsitearchitecture.existingdeveloperswillbemoreproductivebecausetheyareaddingtosomethingtheyarealreadyfamiliarwith,ratherthanhavingtostartfromscratchwithnewtechnology.
asoap-baseddesignmaybeappropriatewhen
aformalcontractmustbeestablishedtodescribetheinterfacethatthewebserviceoffers.thewebservicesdescriptionlanguage(wsdl)describesthedetailssuchasmessages,operations,bindings,andlocationofthewebservice.
thearchitecturemustaddresscomplexnonfunctionalrequirements.manywebservicesspecificationsaddresssuchrequirementsandestablishacommonvocabularyforthem.examplesincludetransactions,security,addressing,trust,coordination,andsoon.mostreal-worldapplicationsgobeyondsimplecrudoperationsandrequirecontextualinformationandconversationalstatetobemaintained.withtherestfulapproach,developersmustbuildthisplumbingintotheapplicationlayerthemselves.
thearchitectureneedstohandleasynchronousprocessingandinvocation.insuchcases,theinfrastructureprovidedbystandardssuchaswsrmandapissuchasjax-wswiththeirclient-sideasynchronousinvocationsupportcanbeleveragedoutofthebox.
restfulsupportinjax-ws
thejavaapiforxmlwebservices(jax-ws)providesfullsupportforbuildinganddeployingrestfulwebservices.theapiwasdevelopedthroughthejavacommunityprocessprogramasjsr224.itistightlyintegratedwiththejavaarchitectureforxmlbinding(jaxb)forbindingxmltojavatechnologydataandisincludedinboththejavaplatform,standardedition(javase)6andthejavaplatform,enterpriseedition(javaee)5.
buildingrestfulservices
theexampleusedinthissectionbuildsonthepurchaseorderservicedescribedinthejavaeeblueprintscatalogasthe"patternsanddesign"whitepapers(parts1,2,and3).theoriginalendpointcontainedonlyonemethod--acceptpo--thatacceptedapurchaseorderandreturnedapurchaseorderstatus,bothofwhichweredefinedbyindividualschemas.
table2ssomemorecrudoperationstoaddtothisservice.
table2:additionalcrudoperations
descriptionjavatechnologymethodsignature
aneworderpublicpurchaseorderstatusacceptpo(purchaseorderorder)
retrieveanexistingorderpublicpurchaseorderretrievepo(stringorderid)
modifyanexistingorderpublicpurchaseorderpo(purchaseorderorder)
cancelanorderalreadysubmittedpublicvoidcancelpo(stringorderid)
jax-wsenablesbuildingrestfulendpointsthroughajavax.xml.ws.providerinterfaceintheapi.providerisagenericinterfacethatcanbeimplementedbyaclassasadynamicnativetoaserviceendpointinterface(sei),andaserviceimplementingthisinterfacecanbedeployedinajavaeecontainerorpublishedinastand-alonemodethroughthejax-wsendpointapi.theproviderinterfacecontainsasinglemethodwiththefollowingsignature:
tinvoke(trequest)
providerisalow-levelgenericapi,butusingitrequirestheendpointtohaveanintimateknowledgeofthedesiredmessageorpayloadstructurebeingpassedtotheservice.dependingonhowtheproviderisimplemented,thesupportedtypesfortandtheirusesarethefollowing:
☆、第五十九章古树
insoftwareengineering,thetermsoftwarearchitecturalstylegenerallyrefersto"asetofdesignrulesthatidentifythekindsofcomponentsandconnectorsthatmaybeusedtocomposeasystemorsubsystem."*somecommonexamplesofarchitecturalstylesincludethepipeandfilter,layered,pushbased,andsoon.inthewebservicesworld,representationalstatetransfer(rest)isakeydesignidiomthatembracesastatelessclient-serverarchitectureinwhichthewebservicesareviewedasresourcesandcanbeidentifiedbytheirurls.webserviceclientsthatwanttousetheseresourcesaccessaparticularrepresentationbytransferringapplicationcontentusingasmallgloballydefinedsetofremotemethodsthatdescribetheactiontobeperformedontheresource.restisananalyticaldescriptionoftheexistingwebarchitecture,andthustheinterplaybetweenthestyleandtheunderlyinghttpprotocolappearsseamless.
thehttpmethodssuchasgetandpostaretheverbsthatthedevelopercanusetodescribethenecessary,read,,and(crud)actionstobeperformed.somemayseeananalogytooperationsinsql,whichalsoreliesonafewcommonverbs,asnintable1.however,thereststyleandhttpprotocolaremutuallyexclusive,andrestdoesnotrequirehttp.
table1:relationshipsbetweensqlandhttpverbs
actionsqlhttp
putreadgetpost
whentouserest
architectsanddevelopersneedtodecidewhenthisparticularstyleisanappropriatechoicefortheirapplications.arestfuldesignmaybeappropriatewhen
thewebservicesarecompletelystateless.agoodtestistoconsiderwhethertheinteractioncansurvivearestartoftheserver.
acachinginfrastructurecanbeleveragedforperformance.ifthedatathatthewebservicereturnsisnotdynamicallygeneratedandcanbecached,thenthecachinginfrastructurethatwebserversandotherintermediariesinherentlyprovidecanbeleveragedtoimproveperformance.however,thedevelopermusttakecarebecausesuchcachesarelimitedtothehttpgetmethodformostservers.
theserviceproducerandserviceconsumerhaveamutualunderstandingofthecontextandcontentbeingpassedalong.becausethereisnoformalwaytodescribethewebservicesinterface,bothpartiesmustagreeoutofbandontheschemasthatdescribethedatabeingexchangedandonwaystoprocessitmeaningfully.intherealworld,mostcommercialapplicationsthatexposeservicesasrestfulimplementationsalsodistributeso-calledvalue-addedtoolkitsthatdescribetheinterfacestodevelopersinpopularprogramminglanguages.
bandwidthisparticularlyimportantandneedstobelimited.restisparticularlyusefulforlimited-profiledevicessuchaspdasandmobilephones,forwhichtheoverheadofheadersandadditionallayersofsoapelementsonthexmlpayloadmustberestricted.
webservicedeliveryoraggregationintoexistingwebsitescanbeenabledeasilywitharestfulstyle.developerscanusetechnologiessuchasasynchronousjavascriptwithxml(ajax)andtoolkitssuchasdirectwebremoting(dwr)toconsumetheservicesintheirwebapplications.ratherthanstartingfromscratch,servicescanbeexposedwithxmlandconsumedbyhtmlpageswithoutsignificantlyrefactoringtheexistingwebsitearchitecture.existingdeveloperswillbemoreproductivebecausetheyareaddingtosomethingtheyarealreadyfamiliarwith,ratherthanhavingtostartfromscratchwithnewtechnology.
asoap-baseddesignmaybeappropriatewhen
aformalcontractmustbeestablishedtodescribetheinterfacethatthewebserviceoffers.thewebservicesdescriptionlanguage(wsdl)describesthedetailssuchasmessages,operations,bindings,andlocationofthewebservice.
thearchitecturemustaddresscomplexnonfunctionalrequirements.manywebservicesspecificationsaddresssuchrequirementsandestablishacommonvocabularyforthem.examplesincludetransactions,security,addressing,trust,coordination,andsoon.mostreal-worldapplicationsgobeyondsimplecrudoperationsandrequirecontextualinformationandconversationalstatetobemaintained.withtherestfulapproach,developersmustbuildthisplumbingintotheapplicationlayerthemselves.
thearchitectureneedstohandleasynchronousprocessingandinvocation.insuchcases,theinfrastructureprovidedbystandardssuchaswsrmandapissuchasjax-wswiththeirclient-sideasynchronousinvocationsupportcanbeleveragedoutofthebox.
restfulsupportinjax-ws
thejavaapiforxmlwebservices(jax-ws)providesfullsupportforbuildinganddeployingrestfulwebservices.theapiwasdevelopedthroughthejavacommunityprocessprogramasjsr224.itistightlyintegratedwiththejavaarchitectureforxmlbinding(jaxb)forbindingxmltojavatechnologydataandisincludedinboththejavaplatform,standardedition(javase)6andthejavaplatform,enterpriseedition(javaee)5.
buildingrestfulservices
theexampleusedinthissectionbuildsonthepurchaseorderservicedescribedinthejavaeeblueprintscatalogasthe"patternsanddesign"whitepapers(parts1,2,and3).theoriginalendpointcontainedonlyonemethod--acceptpo--thatacceptedapurchaseorderandreturnedapurchaseorderstatus,bothofwhichweredefinedbyindividualschemas.
table2ssomemorecrudoperationstoaddtothisservice.
table2:additionalcrudoperations
descriptionjavatechnologymethodsignature
aneworderpublicpurchaseorderstatusacceptpo(purchaseorderorder)
retrieveanexistingorderpublicpurchaseorderretrievepo(stringorderid)
modifyanexistingorderpublicpurchaseorderpo(purchaseorderorder)
cancelanorderalreadysubmittedpublicvoidcancelpo(stringorderid)
jax-wsenablesbuildingrestfulendpointsthroughajavax.xml.ws.providerinterfaceintheapi.providerisagenericinterfacethatcanbeimplementedbyaclassasadynamicnativetoaserviceendpointinterface(sei),andaserviceimplementingthisinterfacecanbedeployedinajavaeecontainerorpublishedinastand-alonemodethroughthejax-wsendpointapi.theproviderinterfacecontainsasinglemethodwiththefollowingsignature:
tinvoke(trequest)