Trait in RAML

Trait in RAML is used to define the same elements in different request URIs so that they can be reused. It’s like common class, common method in Java!

For example, I have 2 request URIs with the following definitions:

In these two requests, the request parameter “name” is defined the same. Therefore, we can use Trait to define this request parameter and reuse it in 2 requests. When we change the definition for the request parameter “name”, we don’t need to edit many places anymore.

We define the Trait for the request parameter “name” with the name “hasNameParam” as follows:

And to use it in URI requests, you need to declare it with the “is” keyword as follows:

Import this API specs into Postman, you will see the following results:

Add Comment