DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Integrating Salesforce APEX REST
  • MuleSoft Integrate With ServiceNow
  • DataWeave: Play With Dates (Part 1)
  • Migrating AnypointMQ-Based Mulesoft Service to Serverless World

Trending

  • Build Your Own Programming Language
  • Elevate Your Terminal Game: Hacks for a Productive Workspace
  • Enhancing Performance With Amazon Elasticache Redis: In-Depth Insights Into Cluster and Non-Cluster Modes
  • Automated Data Extraction Using ChatGPT AI: Benefits, Examples
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Mulesoft OData

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.

By 
Karan Gupta user avatar
Karan Gupta
·
Jan. 04, 22 · Tutorial
Like (6)
Save
Tweet
Share
9.1K Views

Join the DZone community and get the full member experience.

Join For Free

This 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.Installation screenshot.
    • 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.
      • http://studio.mulesoft.org/s4/apikit-for-odata/Plugin installation. Plugin installation screenshot 2.

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. 

  1. 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.
    1. 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


  2. 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
    API screenshot.
  3. 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.Mule generate screenshot.
  4. 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.Content screenshot.
     
    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

  5. 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.Generated flows screenshot.
  6. 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:Output screenshot. Code screenshot.
  7. Once done with the implementation, run the code locally and use the below URL to get the required output as needed.
    1. To call Rest API: Rest API.
    2. To get the description of OData Service:Description code screenshot.
  8. To get the odata Service metadata:  Service metadata screenshot.
  9. To use Query for odata Service:  Query service screenshot.
MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • Integrating Salesforce APEX REST
  • MuleSoft Integrate With ServiceNow
  • DataWeave: Play With Dates (Part 1)
  • Migrating AnypointMQ-Based Mulesoft Service to Serverless World

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: