HTTP地址的规范

Addressing refers to locating a resource or multiple resources lying on the server. It is analogous to locate a postal address of a person.

Each resource in REST architecture is identified by its URI, Uniform Resource Identifier. A URI is of following format:

<protocol>://<service-name>/<ResourceType>/<ResourceID>

Purpose of an URI is to locate a resource(s) on the server hosting the web service. Another important attribute of a request is VERB which identifies the operation to be performed on the resource. For example, in RESTful Web Services - First Application tutorial, URI ishttp://localhost:8080/UserManagement/rest/UserService/users and VERB is GET.

Constructing a standard URI
Following are important points to be considered while designing a URI:

1.Use Plural Noun 

                     - Use plural noun to define resources. For example, we've used users to identify users as a resource.

2.Avoid using spaces 

                     - Use underscore(_) or hyphen(-) when using a long resource name, for example, use authorized_users instead of authorized%20users.

3.Use lowercase letters 

                     - Although URI is case-insensitive, it is good practice to keep url in lower case letters only.


4.Maintain Backward Compatibility 

                     - As Web Service is a public service, a URI once made public should always be available. In case, URI gets updated, redirect the older URI to new URI using HTTP Status code, 300.

5.Use HTTP Verb 

                     - Always use HTTP Verb like GET, PUT, and DELETE to do the operations on the resource. It is not good to use operations names in URI.

Example
Following is an example of a poor URI to fetch a user.

http://localhost:8080/UserManagement/rest/UserService/getUser/1

Following is an example of a good URI to fetch a user.

http://localhost:8080/UserManagement/rest/UserService/users/1

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值