Mulesoft OData
This article guides you on how to implement an OData v2 API. Read on below to find out more on how to complete this yourself.
Join the DZone community and get the full member experience.
Join For FreeThis article will help you build an OData Rest API. Before reading, below are a few prerequisites to be taken care of:
- Anypoint Studio 7.1.4 or later. (Download Link: Anypoint Studio).
- Mule Runtime Engine 4.1.1 or later.
- Install OData Plugin in-studio below are the steps:
- Open Anypoint Studio --> Help --> Install New Software.
- Click on Add option and Add below URL in the location section and then select the OData v2 Plugin to install it in Anypoint Studio.
Let's start now with the development; we will be working on a simple use case for creating an OData Rest API for Salesforce Opportunity Object.
- The first step is to create the OData Model i.e. odata.raml file. This Model structure will have the dataType definition along with the properties for each field. Here is the link for the list of dataType you can define.
- Link
#%RAML 1.0 Library uses: odata: libraries/odataLibrary.raml types: Opportunity: (odata.remote): Opportunity properties: CurrencyIsoCode: type: number (odata.key): true (odata.nullable): false format: int32 required: false Description: type: string (odata.nullable): false (odata.key): true maxLength: 40 Fiscal: type: string (odata.nullable): true (odata.key): false FiscalQuarter: type: number (odata.nullable): true (odata.key): false format: int16 FiscalYear: type: number (odata.nullable): true (odata.key): false format: int16 ForecastCategory: type: string (odata.nullable): true (odata.key): false ForecastCategoryName: type: string (odata.nullable): true (odata.key): false HasOpenActivity: type: boolean (odata.nullable): true (odata.key): false StageName: type: string (odata.nullable): true (odata.key): false CloseDate: type: date-only (odata.nullable): true (odata.key): false Name: type: string (odata.nullable): true (odata.key): false
- Link
- Once the odata.raml file is ready, we can now create a new project in Anypoint Studio and add this file at the below location.
/src/main/resources/api
- Now, it will auto-generate the main raml file(api.raml) and the libraries file i.e. odataLibrary.raml (Library file path defined in above RAML code) by right-clicking on odata.raml file and selecting Mule --> Generate OData API from RAML File.
- Below is the content you can see in the odataLibrary. raml file. It has two things. First, is the Annotation type used by the odata.raml file, and second, is the Query parameters that can be used for Quering the Service.
traits: orderby: queryParameters: orderby: description: Expression for determining what values are used to order the collection of Entries type: string required: false top: queryParameters: top: description: Identifies a subset formed by selecting only the first N items of the set, where N is a positive integer specified by this query option type: number required: false skip: queryParameters: skip: description: Identifies a subset defined by seeking N Entries into the Collection and selecting only the remaining Entries (starting with Entry N+1) type: number required: false filter: queryParameters: filter: description: Identifies a subset determined by selecting only the Entries that satisfy the predicate expression specified by the query option type: string required: false expand: queryParameters: expand: description: A URI with a expand System Query Option indicates that Entries associated with the Entry or Collection of Entries identified by the Resource Path section of the URI must be represented inline type: string required: false format: queryParameters: format: description: If the format query option is present in a request URI it takes precedence over the value(s) specified in the Accept request header. Valid values for the $format query string option are listed in the following table. type: string required: false select: queryParameters: select: description: Specifies that a response from an OData service should return a subset of the Properties which would have been returned had the URI not included a select query option. type: string required: false inlinecount: queryParameters: inlinecount: description: Specifies that the response to the request includes a count of the number of Entries in the Collection type: string required: false
- In the api.raml file, the specification is created based on the data model defined in the odata.raml file. Also, in src/main/mule, the api.xml file is created using scaffolding. This Scaffolding will create separate flows for each method (GET, POST, PUT, DELETE) for the opportunity resource as per our use case. As per our odata model, below will be the flows generated in the api.xml file.
- Now, we can do our implementation to fetch opportunity data from salesforce using get:\Opportunities flow. Below is a sample implementation for the same. For the odata response, you need to follow the defined output structure as below:
- Once done with the implementation, run the code locally and use the below URL to get the required output as needed.
- To get the odata Service metadata:
- To use Query for odata Service:
MuleSoft
Opinions expressed by DZone contributors are their own.
Comments