2012-03-05 34 views
1

我是REST的新手,但是我已经构建了一个简单的Web服务,并且无法找到URL格式正确的简单解释。对于这个简单的REST Web服务,我的URL应该是什么样的?

该服务允许创建发票并通过一系列简单的审批阶段。

(1)阅读在一个XML格式的所有发票:

GET: http://localhost/webapp/ws/invoices 

(2)读取在XML格式的一个发票(例如发票ID = 555):

GET: http://localhost/webapp/ws/invoices/555 

(3 )提交一个新的发票:

POST: http://localhost/webapp/ws/invoices 

随着发票属性( “参数userid”, “totalprice” 等)都包括像的POST参数一个简单的HTML表单。

(4)批准的发票:

POST: http://localhost/webapp/ws/invoices/action 

随着动作属性(例如 “用户ID = 123”,invoiceid = 567, “行动= APPROVE” 或 “拒绝”,等等)都包括像一个简单的HTML表单的POST参数。

它工作正常,但它甚至接近REST风格的Web服务应该是什么样子?

任何意见非常感谢,谢谢。

罗布

回答

1

恕我直言,在审批过程中,我会做:

(4)核准的发票:

PUT: http://localhost/webapp/ws/invoices/555 

当你要修改现有的资源,由ID标识(555),所以你只需要传递将改变的属性。

+0

谢谢,这是一个好主意。 – 2012-03-06 13:59:03

3

这些URL不会生成API RESTful或不。清楚地表示您的资源及其状态转换(通过链接和表单)并避免将客户端连接到您的实施的带外信息更为重要。这个概念很好。

1)创建一个根资源,为所有客户端提供一个非常熟悉的起点,并允许他们发现可用的服务(这可以与Browers使用的URL相同)使用Accept头来确定HTML或您的API媒体类型应该返回)。

GET:http://localhost/webapp

<webapp href="/webapp"> 
    <invoices href="/webapp/invoices"/> 
    ... any other services ... 
</webapp> 

2)创建您的发票

集合资源

GET:http://localhost/webapp/invoices

<invoices href="/webapp/invoices"> 
    <invoice href="/webapp/invoices/555"/> 
    <invoice href="/webapp/invoices/554"/> 
    <invoice href="/webapp/invoices/553"/> 
    <invoice href="/webapp/invoices/552"/> 
    ... 
    <search href="/webapp/invoices/" method="get"> 
     <query type="xpath" cardinality="required"/> 
    </search> 
    <next href="/webapp/invoices?page=2" method="get"/> 
    <create-draft href="/webapp/invoices" method="post"> 
     <total-price type="decimal" cardinality="optional"/> 
     ... user should be picked up automatically based on the authorised user posting the form ... 
     ... add other optional and required parameters here. ... 
    </create-draft> 
</invoices> 

这是一个分页集合,与next元素告诉客户端如何获得下一页。如果没有足够的发票(例如,,5张发票,每页可以包含10)那么next元素将不会显示。同样,如果请求者无权创建发票,那么将不包括create-draft表单。

获取下一个页面看起来是这样的:

GET:http://localhost/webapp/invoices?page=2

<invoices href="/webapp/invoices"> 
    <invoice href="/webapp/invoices/545"/> 
    <invoice href="/webapp/invoices/544"/> 
    <invoice href="/webapp/invoices/543"/> 
    <invoice href="/webapp/invoices/542"/> 
    ... 
    <search href="/webapp/invoices/" method="get"> 
     <query type="xpath" cardinality="required"/> 
    </search> 
    <next href="/webapp/invoices?page=3" method="get"/> 
    <prev href="/webapp/invoices" method="get"/> 
    <create-draft href="/webapp/invoices" method="post"> 
     <total-price type="xs:decimal" cardinality="optional"/> 
     ... user should be picked up automatically based on the authorised user posting the form ... 
     ... add other optional and required parameters here. ... 
    </create-draft> 
</invoices> 

3)创建发票的项目资源

GET:http://localhost/webapp/invoices/555

<invoice href="/webapp/invoice/555"> 
    ... details go here ... 
    <reject href="/webapp/invoices/555" method="delete"> 
     <reason type="xs:string" cardinality="required"/> 
    </reject> 
    <approve href="/webapp/invoices/555" method="put"> 
     ... add approval parameters here ... 
    </approve> 
</invoices> 

同样,如果用户没有被授权重新使用ject或批准发票,那么这些元素不应该被显示。如果发票已被批准(在这种情况下可能有取消表单),也是如此。

相关问题