RESTful Web Services - Addressing

RESTful Web Services - Addressing

Addressing refers to the retrieval of a resource or multiple resources residing on a server. This is similar to looking up a person's postal address.

Each resource in the REST architecture is identified by its URI (Uniform Resource Identifier). The URI has the following format −

:////

The purpose of the URI is to locate the resource(s) on the server hosting the web service. Another important request attribute is VERB, which identifies the operation to be performed on the resource. For example, in the RESTful Web Services chapter - the first app URI is http://localhost:8080/UserManagement/rest/UserService/users and the VERB is GET.

Building a Standard URI

The following are important points to consider when designing a URI:

  • Use Plural - Use the plural to identify resources. For example, we used users to identify users as a resource.

  • Avoid spaces - use an underscore (_) or a hyphen (-) when using a long resource name. For example, use author_users instead of authorized %20users.

  • Use lowercase letters - Although URIs are case insensitive, it is recommended that you store URLs in lowercase letters only.

  • Maintain backwards compatibility - Because the web service is public, the URI that was once exposed should always be available. If updating the URI, redirect the old URI to the new URI using the 300 HTTP status code.

  • Use HTTP Verb - Always use an HTTP verb such as GET, PUT, and DELETE to perform operations on a resource. It is not correct to use the operation name in a URI.

Use Plural - Use the plural to identify resources. For example, we used users to identify users as a resource.

Avoid spaces - use an underscore (_) or a hyphen (-) when using a long resource name. For example, use author_users instead of authorized %20users.

Use lowercase letters - Although URIs are case insensitive, it is recommended that you store URLs in lowercase letters only.

Maintain backwards compatibility - Because the web service is public, the URI that was once exposed should always be available. If updating the URI, redirect the old URI to the new URI using the 300 HTTP status code.

Use HTTP Verb - Always use an HTTP verb such as GET, PUT, and DELETE to perform operations on a resource. It is not correct to use the operation name in a URI.

example

Below is an example of a bad URI for a user fetch.

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

Below is an example of a good URI for user fetch.