Web Service - RESTful

What is RESTful

Representational State Transfer (表现层状态转化)

an architecture design that define a stateless client where web services are treated like resources and can be accessed and identified by URL

RESTful架构,是目前流行的一种互联网软件架构

组合起来就是,基于Client-Server架构,使用HTTP动词,对资源进行操作/传递。

http://www.ruanyifeng.com/blog/2011/09/restful.html

REST v.s. SOAP

Example: querying a phonebook application for the details of a given user using user’s ID.

use REST

http://www.example.com/phonebook/UserDetails/12345

use Web services and SOAP

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
 <soap:body pb="http://www.acme.com/phonebook">
  <pb:GetUserDetails>
   <pb:UserID>12345</pb:UserID>
  </pb:GetUserDetails>
 </soap:Body>
</soap:Envelope>

Differences between SOAP WS and RESTful WS

When to use REST?

When to use SOAP?

其实,说了这么多,总结起来就一句话,除非万不得已一定要用SOAP,其它情况都用RESTful。而什么情况是非用SOAP不可呢?那就是Stateful operations。 至于这种带状态的网络请求,设计上是否合理,那就是另一个话题了。(有一种叫做legecy application的东西)

Server Response Format

<parts-list>
 <part id="3322">
  <name>ACME Boomerang</name>
  <desc>
        Used by Coyote in <i>Zoom at the Top</i>, 1962
  </desc>
  <price currency="usd" quantity="1">17.32</price>
  <uri>http://www.acme.com/parts/3322</uri>
 </part>
 ...
</parts-list>

Example

JAX-RS with Jersey

e.g. RESTfulDemo.zip, RESTfulClient.zip

Steps to Create RESTful Services:

Steps to Consume RESTful Services:

http://www.java2blog.com/2013/04/create-restful-web-servicesjax-rs-using.html

REST with Spring

e.g. ConsumeRestSpring-demo.zip, BuildRestSpring-demo.zip

Steps to Consume Spring RESTful Services

Steps to Build Spring RESTful Services

Other Spring-REST Peojects

Testing Tools

Fork me on GitHub