Wanting to implement a REST interface to a service, you first have to figure out the resource structure. Have a look at the structure of the rest example:
Figure: Example Class Diagram of a REST Interface
The rest interface SupportAPI has the following structure:
Resource | Methods | |
---|---|---|
supportcases that accept GET, GET/ and POST. | GET | Get some information on the existing support cases. |
GET/ | Get all support cases. | |
POST | Create a new support case. | |
Underneath the supportcases, there is a single supportcase that can be accessed via its id. | DELETE | Close a support case. |
GET | Get the data of a single support case. | |
resolve | Set the status of the support case to "resolved". | |
Underneath the supportcases as well, there is a customer that can be accessed via its customerID. | GET/ | Get all support cases of that specific customer. |
From the example, you can see implementations of GET, POST and DELETE methods. Of course, you can use the other available methods with the Bridge, as there are PUT, PATCH, HEAD and OPTIONS.
REST resources are generated to the OpenAPI file with their class name only, instead of their fully qualified name (including the xUML package structure, like |
A REST Port Type is a class having stereotype <<E2ERESTPortType>>. A REST port type can be deployed just like any other xUML service. It has the following tagged values:
The REST port type can be added to the component diagram just as any other port type, e.g. SOAP port type:
Note the trace port that can be defined on the <<RESTService>> component. This is a shadow SOAP port that can be used to test the REST methods with the Analyzer.
The <<E2ERESTPortType>> SupportAPI has path /support
applied. The REST service can be accessed via /support
instead of /Services/SupportCase/SupportAPI
as depicted in the containment tree below.
It has a <<RESTError>> class RESTError applied as error class.
A REST Resource is a class having stereotype <<RESTResource>>. This stereotype represents both: collections of resources (e.g. supportcases) and single resources (supportcase). Both are handled indifferently by the Bridge. It is the modeler who should be aware, that some methods may not make sense on collections.
REST Resources have the following tagged values:
Example REST Resource | Description | |
---|---|---|
supportcases | REST resource supportcases has no relative path applied. It will be accessible via /support/supportcases . The first part of the URL is coming from the path value of the REST port type. | |
supportcase | REST resource supportcase has a dynamic path applied: :id. It will be accessible via For more information on REST parameters refer to Defining REST Parameters. | |
customer | REST resource customer has a combined static and dynamic path applied: customer/:customerID. This is necessary to avoid conflicts with supportcase, which also has dynamic elements in its path. |
A REST Method is an method having the stereotype <<REST>>. REST methods must be static.
With REST methods, we distinct between verb methods and named methods.
GET
on /support/supportcases will route to the GET method of class supportcases and give an overview on the existing support cases.PUT
on /support/supportcases/1234/resolve
will route to the resolve method of class supportcases.Verb methods (unlike normal methods) can be in form of Think about the support manager example.
|
REST methods have the following tagged values:
If the method name is one of GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS (with optional trailing '/'), it will be invoked automatically on its parent resource when an corresponding request is received.
Example REST Resource | Method | Descripton | |
---|---|---|---|
supportcases | GET and GET/ | verb method | Both methods have no httpMethod applied as GET is the default method. They will be invoked, when accessed via a GET on /support/supportcases or /support/supportcases/ . |
getByDate | named method | This method has httpMethod GET applied. It will be invoked on a GET on /support/supportcases/date=<a valid date> . | |
POST | verb method | This method has httpMethod POST applied. It will be invoked on a POST on /support/supportcases . | |
supportcase | DELETE | verb method | This method has httpMethod DELETE applied. It will be invoked, when accessed via a DELETE on /support/supportcases/<a support case id> , because its parent resource has a relative path :id applied. |
GET | verb method | This method has no httpMethod applied as GET is the default method. It will be invoked, when accessed via a GET on /support/supportcases/<a support case id> . | |
resolve | named method | This method has httpMethod PUT applied. It will be invoked, when accessed via a PUT on /support/supportcases/<a support case id>/resolve . | |
customer | GET/ | verb method | This method has no httpMethod applied as GET is the default method. It will be invoked, when accessed via a GET on /support/supportcases/customer/<a customer id> , because its parent resource has a relative path :customerID applied. |
A REST Parameter is an input parameter of a <<REST>> method having the stereotype <<RESTParameter>>. This defines that this parameter will be provided via path, query, body, or header of the HTTP request. This has to be indicated on the parameter by setting tagged value in:
All path parameters are required. For all other parameters, use the multiplicity to specify whether they are required or not.
Example REST Resource | REST Parameter | Tagged Value "in" | Remark |
---|---|---|---|
supportcases | status, customerName | query | status and customerName are provided via the query string: /support/supportcases/?status=in%20progress . In this case, the xUML Runtime will automatically assign the parameters coming with the query string to the REST parameters. |
supportCase | body | For posting a new support case, the support case data supportCase is provided through the HTTP body. In this case, the xUML Runtime will automatically assign the data from the embodied JSON or XML document to the REST parameter class. | |
supportcase | id | path | REST resource supportcase has a dynamic path :id applied. For this reason, all methods of this resource must have a REST parameter with the same name id that will receive the value from the URL. |
REST services in general return errors via the HTTP status code, so first of all, you should carefully choose the status code you are returning on a service call. Besides the HTTP status code there is no standard way of how to provide additional error information with REST service implementations. Developers can return additional information in HTTP headers or body, though. With the Bridge REST implementation, we decided to provide error information via the HTTP body by an error class or a Blob. |
Default Error ClassEach REST port type should have a default <<RESTError>> class assigned. The Bridge will use this class as a default output in case of error. Figure: Example REST Error Class In case of error, this class should be
Refer to Implementing REST Operations for more information on error handling. Specific Error ClassesYou can define specific error classes for specific HTTP errors to provide more information on the error, or just return a Blob. Figure: Specific Error Class
There is no difference between using an error class or a Blob. Assign the specific error class to related operations or REST resources via a <<use>> dependency having stereotype <<RESTResponseDefinition>>. Figure: Assigning Error Class to REST Operations On these <<use>> dependencies, you have to specify an HTTP status code on the name tag. For this status code, the default error class will be overwritten by the specific class.
You can apply the following name templates:
The definitions above are reflected in the OpenAPI service description (see REST Response Definitions).
For responses of type Blob, you can additionally specify a blob body content type on the <<RESTResponseDefinition>> (tag blobBodyContentType). This information will be generated to the OpenAPI descriptor file and will set the the "Content-Type" header to this content type. Default content type is "application/octet-stream". Refer to Implementing REST Operations for more information on error handling. |