0
votes

I have created RAML and example JSON file inside Anypoint platform and have generated flow using RAML file that is the APIKit router. Here is my RAML file content:

 #%RAML 0.8
 ---
 title: TestEmployee API
 version: v1

 /employee:
   get:
    queryParameters:
      id:
       displayName: Employee id
       required: true
      name:
       displayName: Employee name
       required: false
    responses:
     200:
      body:
        application/json:
         example: !include EmployeeExample.json

And example JSON is:

[{
"id": 1,
"name":"Charles",
"code": "C1ENU00",
"dateofjoining":"2019/06/24",
"domain":"ENU",
"address":"Hyderabad",
"phone": 9865458936,
"program": "WASE"
},
{
"id": 2,
"name":"John",
"code": "C2DIG00",
"dateofjoining":"2019/06/24",
"domain":"DIGITAL",
"address":"Chennai",
"phone": 9756359864,
"program": "ELITE"
}
]

I want to use a Choice router to route messages according to the condition so that 'id' and 'name' from query parameters would give JSON responses containing that 'id' and 'name' only. Please guide me in creating appropriate flows and Dataweave expressions if any.

1

1 Answers

1
votes

You don't need a choice router just for that. APIKit is a router itself, which routes according to the method and endpoint from the URL. When you scaffold the flows using the RAML it creates a dummy flow for the GET employee endpoint. You just need to replace the contents of that flow with the logic to query the information from some where, probably using a connector.

Example: The autogenerated flow by Studio will have a name like "get:\employee:application\json:myApp". You can replace the default content by a DataWeave transformation to return the list specified:

 <flow name="get:\employee:application\json:myApp">
        <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
            <ee:message>
                <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
[
   {
    id: 1,
    name:"Charles",
    code: "C1ENU00",
    dateofjoining:"2019/06/24",
    domain:"ENU",
    address:"Hyderabad",
    phone: 9865458936,
    program: "WASE"
   },
   {
    id: 2,
    name:"John",
    code: "C2DIG00",
    dateofjoining:"2019/06/24",
    domain:"DIGITAL",
    address:"Chennai",
    phone: 9756359864,
    program: "ELITE"
   }
]
]]></ee:set-payload>
                </ee:message>
            </ee:transform>
        </flow>