What Is RAML?
RAML, which stands for “RESTful API Modeling Language,” is a YAML-based language for describing RESTful APIs. It’s a blueprint earlier than creating an API that makes it simpler for each builders and stakeholders to know the API’s construction and conduct.
What Are Information Sorts?
Data types are used to specify the construction or sort of knowledge exchanged between the consumer and server. This enables to validate the request and response payloads and preserve payload requirements.
Constructed-In Information Sorts
RAML consists of a number of built-in or primitive knowledge sorts that symbolize primary kinds of values. A number of the frequent built-in knowledge sorts embrace:
- String: Represents textual knowledge.
- Quantity: Represents numeric values.
- Integer: Represents entire numbers.
- Boolean: Represents true or false values.
- Date-Solely: Represents a date and not using a time part.
- Time-Solely: Represents a time and not using a date part.
- DateTime: Represents a date and time.
Consumer-Outlined Information Sorts
You may also outline customized or user-defined knowledge sorts in RAML. These sorts can have particular properties with their very own knowledge sorts. This lets you create structured and reusable elements on your API.
Use Case
Create a RAML with a single useful resource with completely different knowledge sorts and codecs and publish it on Alternate.
Objectives
- Create a RAML Specification.
- Create Datatype information.
- Create a useful resource /consumer and add the required data.
- Confirm endpoints utilizing mocking providers.
- Publish to Alternate.
Step 1: Create RAML Specification
- Create an account on the Anypoint Platform.
- Navigate to the Design Heart and create a RAML Specification.
- Give an applicable title to your specification.
Step 2: Create DataType Information
- Create a folder “dataTypes” within the information part.
- Create a folder dataTypes and add request and response knowledge sorts as under.
- jsonDataTypeOne
- jsonDataTypeTwo
- xmlDataType
- responseDataType
jsonDataTypeOne
#%RAML 1.0 DataType
sort: object
properties:
title:
sort: string
instance: "Caroline"
age:
sort: quantity
instance: 22
energetic:
sort: boolean
instance: true
jsonDataTypeTwo
#%RAML 1.0 DataType
sort: object
properties:
title:
sort: string
instance: "Caroline"
age:
sort: quantity
instance: 22
handle:
sort: string
instance: "145 Most important St, Dallas, TX"
xmlDataType
#%RAML 1.0 DataType
sort: object
properties:
prime:
sort: object
properties:
abc:
xml:
attribute: true
title: abc
xmlNamespace:
xml:
attribute: true
namespace: http://google.com
node:
sort: string
instance:
<xml>
<prime abc="123" xmlns="http://google.com">
<node>12345</node>
</prime>
</xml>
responseDataType
#%RAML 1.0 DataType
sort: object
properties:
message:
sort: string
instance: "success"
payload:
sort: object
instance: {}
Step 3: Create Useful resource/Customers
Under is the pattern RAML specification.
#%RAML 1.0
title: check
sorts:
jsonTypeOne: !embrace dataTypes/jsonDataTypeOne.raml
jsonTypeTwo: !embrace dataTypes/jsonDataTypeTwo.raml
xmlType: !embrace dataTypes/xmlDataType.raml
responseType: !embrace dataTypes/responseDataType.raml
/customers:
publish:
physique:
software/json:
sort: jsonTypeOne | jsonTypeTwo
software/xml:
sort: xmlType
responses:
200:
physique:
software/json:
sort: responseType
Within the above RAML specification, “|” image is used within the physique sort, which implies that it will probably settle for both jsonTypeOne or jsonTypeTwo. Along with this we are able to additionally ship xmlType.
Step 4: Confirm Endpoints Utilizing Mocking Providers
Within the above picture, we are able to see that it accepts each json sort and xml sort. Additionally it permits consumer to decide on between jsonTypeOne or jsonTypeTwo.
Step 5: Publish to Alternate
![publish to exchange](https://dz2cdn1.dzone.com/storage/temp/17547771-1709655161252.png)
As soon as the RAML specification is prepared and verified utilizing mocking providers, you’ll be able to publish the specification to the Anypoint Alternate. Publishing to Alternate will allow your customers to confirm the endpoints from their finish earlier than beginning the implementation.
Abstract
In conclusion, a single useful resource can have completely different knowledge sorts with completely different codecs and customers can implement them as per their necessities.