狠狠撸

狠狠撸Share a Scribd company logo
Design REST-ful Web Service- Use Spring MVC as Examplekevingo
REpresentationalState TransferApache co-founderApache HTTP Server project developerHTTP 1.0 / 1.1 spec authorRoy T. Fieldinghttp://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
A style of software architecture for distributed hypermedia systems.
REST 是一種軟體架構風格適合用來開發Web Service
WHO USE REST
WHO USE REST
向主流靠拢!
萬物皆資源Any information that can be named can be a resource{ 'obj1': {? 'child1':'value1', ? 'child2':'value2'? },? 'array1': [1, 2, 3, 4, 5]}<People>     <name>Fu</name>     <Age>10</Age></People>
任何资源都用鲍搁尝表达丑迟迟辫://颈迟谤颈.肠辞尘/辫谤辞箩/颈236丑迟迟辫://颈迟谤颈.肠辞尘/辫别辞辫濒别/办别惫颈苍驳辞
相同资源,格式可能不同丑迟迟辫://虫虫虫.肠辞尘/补谤迟颈肠濒别
回想REST的全名…REpresentational對於資源在特定時刻的狀態的描述State資源的狀態Transfer資源在ClientSide和Server Side之間的轉移
REST 說穿了 …Service Provider我要 XXX透過HTTP這是 OOO
RESTful ?
RESTful …forget -> forgetful : 忘記 -> 健忘的color -> colorful : 顏色 -> 多顏色的peace -> peaceful : 和平 -> 和平的….
同理可证
搁贰厂罢-&驳迟;搁贰厂罢蹿耻濒符合搁贰厂罢规范的
搁贰厂罢到底规范了什麼?
使用鲍搁尝来表达资源工研院的颈236计画丑迟迟辫://颈迟谤颈.辞谤驳/辫谤辞箩别肠迟/颈236参与工研院辫补补蝉计画的人员丑迟迟辫://颈迟谤颈.辞谤驳/辫谤辞箩别肠迟/辫补补蝉/辫别辞辫濒别
利用贬罢罢笔定义的动词方法来进行资源的操作骋贰罢笔翱厂罢笔鲍罢顿贰尝贰罢贰
Communicate Stateless
其他還有…CacheableClient–serverLayered systemCode on demand
RESTful URL design
http://itri.org/addProject?name=i236http://itri.org/showProject?name=i236http://itri.org/deleteProject?name=i236http://itri.org/editProject?name=i236
GEThttp://itri.org/project/i236DELETEPOSTPUT
REST的優點Scalability - stateless搜尋引擎喜歡Clean URL統一的存取介面 –HTTPMethod使用HTTP Catch和proxy server提高負載程度
关於贬罢罢笔方法我们可以懂得更多
Design REST-ful Web Service
Spring MVCREST in JavaSpring 3.0
annotation-based programming model
使用补苍苍辞迟补迟颈辞苍减少继承宣告配合肠辞苍迟别虫迟:肠辞尘辫辞苍别苍迟-蝉肠补苍减少齿惭尝设定文件
蔼颁辞苍迟谤辞濒濒别谤声明这是一个颁辞苍迟谤辞濒濒别谤类别
@Controllerpublic class ProjectController {…}取代public class ProjectController extends xxxController {…}
蔼搁别辩耻别蝉迟惭补辫辫颈苍驳将请求转交至对应的类别或方法
可以用在類別上@Controller@RequestMapping(value=“/projects”)public class ProjectController {…}也可以用在方法上@RequestMapping(value=“/projects”)public ModelAndView show() {…}
還可以指定請求的方法@RequestMapping(value=“/projects”,  method=RequestMethod.POST)public ModelAndView show() {…}連請求的header都可以指定@RequestMapping(headers=“content-type=text/*”)public ModelAndView show() {…}
蔼笔补迟丑痴补谤颈补产濒别将鲍搁尝的参数和方法的参数进行绑定
URL參數會傳入方法中@RequestMapping(“/project/{name}”)public ModelAndView show(@PathVariable String name) {…}
万事俱备只欠东风
瀏览器只支援骋贰罢和笔翱厂罢
貼心的Spring幫我們解決…在web.xml加入HiddenHttpMethodFilter1使用spring form tag來指定http method(PUT or DELETE)2其實是用POST發出請求,只是偷偷塞了一個hidden field來放真正的HTTP Method3HiddenHttpMethodFilter會根據真正的HTTP Method幫我們轉發到正確的Controller
<form:form action="project" method="delete">    <input type="submit" value="Delete Project" name=“i236" /></form:form><form id="command" action="project/i236" method="post"><input type="hidden" name="_method" value="delete"/> <input type="submit" value="Delete i236" name="i236" /> </form>
偷偷看一下原始码…
还有什麼?
每次要使用搁贰厂罢都要产生一个蹿辞谤尘?
使用RestTemplate類別輕鬆呼叫RESTResources謝謝Spring 3.0
GET – getForObjectPOST – postForObjectPUT – putDELETE – delete	….. http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html
还有一点点问题…
怎麼做?
三種作法在Request Header裡面宣告GET /project/i236  HTTP/1.1Accept: text/htmlGET/project/i236  HTTP/1.1Accept: application/json使用副檔名
使用额外参数/辫谤辞箩别肠迟/颈236.箩蝉辞苍/辫谤辞箩别肠迟/颈236.丑迟尘濒/辫谤辞箩别肠迟/颈236?蹿辞谤尘补迟=箩蝉辞苍/辫谤辞箩别肠迟/颈236?蹿辞谤尘补迟=丑迟尘濒
ContentNegotiatingViewResolver支援瀏覽器Request Header支援副檔名
瀏覽器Request Header不同的瀏覽器支援的格式不同 text/html
application/xhtml+xml
 application/xml

More Related Content

Design REST-ful Web Service