Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

REST

Introduction

REST is not a standard but a Web application development architectural style, which can be understood as a design pattern. REST-based HTTP, URI, XML and widely popular these existing protocols and standards, along with REST, HTTP protocol to obtain a more correct use. Compared to SOAP and WSDL-based Web services, REST model provides a more concise realization.

REST API using the resource positioning, WEB service interface is provided externally and announced a series of URI parameters.

URI

Introduction

URI(Uniform Resource Identifier) is a string of some simple uniform (standardized) manner that identifies the resource

Description

Part Description

URI generally consists of three parts

  • Named mechanism for accessing resources
  • Storage resource host name
  • The name of the resource itself, represented by the path. Typically, this string scheme (named URI namespace identifier) at the beginning of the following syntax: [scheme:] scheme-specific-part. URI scheme and the beginning of the colon, Scheme uppercase / lowercase letters beginning, followed by the empty or more uppercase / lowercase letters, numbers, plus, minus and dot. Colon and the scheme scheme-specific-part separated, and the scheme-specific-part syntax and semantics (meaning) is determined by the namespace URI.

A typical URI in the table below, including the protocol name, host name, port services, and other resources to address and query string:

NameDescription
specificationscheme://[host]:[port]/path?queryString
schemeProtocol term , typically http or https
host(DNS) Host Name or IP address
portPort
pathResource address, logical Hierarchy Use '/ 'delimited
 ?Resources used to separate address and query string of symbols
queryStringQuery string, scope information ampersand method to separate the query conditions separated by commas orderly scope information separated by semicolons no information about the scope of the order

Resource Address Description

Resource address is divided into ServiceType, ServiceName, Service Version, PathInfo four parts.

http(s)://[hostname][:port]/[ServiceType]/[ServiceName]/[Service Version]/[PathInfo]

ServiceType

Service Type?api and ui(UI)

ServicesName

Service Name, a globally unique

Recommended:

  • For GSO, SDNO and NFVO, service name should include the project name as well as the microservice name to ensure uniqueness, example: 'sdno-l3vpnService'
  • For O-Common and Common-Tosca, the project name is not necessary in the service name, example: 'catalog'

ServicesVersion

Service Version, requirements v + numbers, such as v1 v1.1, etc

PathInfo

Resource Path Information

ServiceTypeServiceNameServiceVersionPathInfo
apicatalogv1/servicetemplates
apicatalogv1/servicetemplates/{serviceTemplateId}
uilogv1/info/syslogs
uilogv1/statisticinfo/syslogs

Absolute URI and Relative URI

Absolute URI: In scheme (followed by a colon) at the beginning of the URI, for example http://www.cnn.com/index.html. It can be seen as an absolute URI reference to a resource in some way, and this way the environment is not dependent identifier appears.
Relative URI: Not in the scheme (followed by a colon) begins URI, such as articles / articles.html. You can put relative URI regarded as quoted in some way some resources, and in this way depends on the identifier appears environments.

Http Method

Operation Method

HTTP / 1.1 Protocol defines eight methods of CPC (sometimes called "action") to indicate a different mode of operation Resources Request-URI specified:

  • OPTIONS :The method returns the server request for a particular resource supports HTTP. It can also be used to send to the Web server '\ *' request to test the WEB server supports.
  • HEAD : Ask the server to ask for consistent response GET request, but the response body will not be returned. This method can be without having to transport the entire content, it can get included in the response message header meta-information.
  • GET : Make a request to a specific resource. Note: GET method should not be used to generate the "side effects" of the operation, such as modifying the resource status. Idempotent
  • POST : Submitting data to the specified resource for processing a request (such as submitting a form or uploading files). Data is included in the request body. POST requests may lead to the establishment and / or existing resources to new resource changes.
  • PUT: To upload their content to the latest specified resource location.
  • PATCH : Requests the server entity designated URI represented by the partial update. PATCH method should contain enough information for the server update requests. If the specified resource exists, the server can be updated and return the status code 200 (OK) or 204 (No Content). it similar to the processing with PUT method , if the specified resource does not exist, the server can create the resource. If the server resource is created, it should return the status code 201 (Created). If a resource to support PATCH method, then OPTIONS response Allow header can this be explained. The server can also use the Accept-Patch headers, lists media types the client can request its sending PATCH. Protocol specification recommends that media type should contain semantic information required to transfer part of the update to the server. json-patch (see https://tools.ietf.org/html/draft-pbryan-json-patch-04) is a proposed draft media types, partial updates required to support the operation expression.
  • DELETE: Requests the server to delete * Request-URI * resources identified.
  • TRACE : Requests the server returns a request it receives. The server returns a content-type of message or http context, which contains the complete request information. Clients can use the TRACE method, review the changes made to the message request message through a proxy and an intermediate layer to help diagnose problems.
  • CONNECT: Reserved for the ability to connect to the pipeline mode proxy server.

Additional Properties

Safe

Use secure HTTP method for sending the request, it does not produce any side effects from the user side. This is not to say that security measures will not cause side effects, but that the user can use this method to safely initiate the request, do not worry about inadvertently modified the system state.

Idempotent

By Idempotent issued a request for resources with multiple requests the same effect. By definition, all security methods are idempotent. Some methods are not safe, but may also be idempotent. Similar security methods, using idempotent request does not guarantee that does not produce any side effects on the server side, but unrelated to these possible side effects and users.

Cachable

Cacheable ways to obtain a response to previous requests from the cache at a mid-tier caching.
MethodSafeIdempotentCachableREST Use
GETYesYesYesQuery Resource
HEADYesYesYes
POSTNoNoNoCreate Resource
PUTNoYesNoUpdate Resource
DELETENoYesNoDelete Resource
OPTIONSYesYesNo
PATCHNoNoNoUpdate Resource
TRACEYesYesNo

When a request for a resource does not support the corresponding request method, the server should return the status code 405 (Method Not Allowed); when the server does not know or does not support the corresponding request method, it shall return a status code 501 (Not Implemented).
HTTP server should implement at least the GET and HEAD methods, other methods are optional. In addition to the above-described method, a specific HTTP server can also extend custom method.

FunctionResource Address
Add/CreatePOST/books
PUT/books/{id}
DeleteDELETE/books
Modify/UpdatePUT /books
Query AllGET /books
Primary key queryGET /books/{id}
GET /books?id=123
Paging scope queryGET /books?start=0&size=10 
GET /books/01,2012-02,2015
GET /books/hadoop;language=scala;type=xxx

Resource Address Design Specification

Service Description

  • API Service
  • Content service , static pages and js, css and other resources
Functionscheme://[host]:[port]/pathqueryString
API Service Specification[[host\]]:[port]/api/[Service Name]/v[Version Number]/[Resource]/queryparam1=xxx, queryparam2=xxx
Content Service Specification[[host\]]:[port]/ui/[Content of the resource]/index.htmlNone

Note: The version number before lowercase letter 'v'

For example:

Functionscheme://[host]:[port]/pathqueryString
log API Service[[host\:\[port\]/api/log/v1/loginfo/syslogs]]starttime=xxxx
Endtime=xxxx
log Content Service[[host\:\[port\]/ui/log/]]
alarm API Service[[host\:\[port\]/api/fm/v1/alarms]]
alarm Content Service[[host\:\[port\]/ui/fm/]]

Use path variables to express the resource hierarchy

For example:

View a device information in topology:

http://127.0.0.1/api/topo/v1/resources/{Resourcelocate id}

View a user information in safe module?

http://127.0.0.1/api/sm/v1/users/{userid}

Path variable expression using punctuation non-hierarchical structure

  • If you need to express non-hierarchical order is irrelevant, use a semicolon.
  • If you need to express non-hierarchical order, use a commas.
  • If the application requires the expression of more complex structures, you can define additional punctuation and semantics, but the comma and semicolon must be used to indicate the order of relations with non-hierarchical structure.

For example:

View all link information between two nes in topology:

http://127.0.0.1/api/topo/v1/links/{ne id1};{ne id2}

View all link information from one ne to another ne in topology:

http://127.0.0.1/api/topo/V1/links/{ne id1},{ne id2}

Use query variable expression algorithm (service) input

For example:

View a system log information

http://127.0.0.1/api/log/v1/syslogs?id=101&filter=admin&count=50

Note: '&' In the URL is the only standard parameters of Http URL delimiters, do not denote any business meaning. Application needs its own resolution parameters of business meaning.

Use json variable expression algorithm (service) input

  • POST request uses json object as a parameter input, you need to put inside the body, parser body when the server response.
  • If the query parameter is too long GET request, allow POST GET equivalent use; This policy requires strict restrictions violate REST principles.

Exception Handling Design

JAX-RS2.0 defines the status code of the response javax.ws.rs.core.Response.Status and base exception class
javax.ws.rs.WebApplicationException, This exception contains the following sub-classes.


Status CodeRequest TypeMeaningException class
200 OK[GET]Server successfully returned the data requested by the user, the operation is idempotentResponse.Status.OK
201 CREATED[POST/PUT/PATCH]?Create or modify user data successfulResponse.Status.CREATED
202 ACCEPTED[*]?Represents a request has been queued into the background (asynchronous task)Response.Status.ACCEPTED
204 NO CONTENT[DELETE]Delete user data successfulResponse.Status.NO_CONTENT
400 BAD REQUEST[POST/PUT/PATCH]?Request issued by a user error, the server does not operate create or modify data, the operation is idempotentBadRequestException
401 Unauthorized[*]Indicates that the user is not authenticated (token, username, password error)NotAuthorizedException
403 Forbidden[*]Indicates that the user is authenticated (401 errors are relatively), but access is prohibited. it used for unauthorizedForbiddenException
404 NOT FOUND[*]Request issued by the user is directed to a nonexistent record, the server does not operate, the operation is idempotent.NotFoundException
406 Not Acceptable[GET]Format requested by the user not available (such as a user request JSON format, but only XML format).NotAcceptableException
410 Gone[GET]Resource requested by the user is permanently deleted and can not be obtainedResponse.Status.GONE
422 Unprocesable entity[POST/PUT/PATCH]When you create an object, a validation error occurs.
500 INTERNAL SERVER ERROR[*]Server error occurs, the user will be unable to determine whether the request is sent successfullyInternalServerErrorException

Discussion

  • No labels