我有一个与RAML发送不同回复有关的问题。在下面的RAML中,我想基于查询参数发送响应。如果我的查询参数(flightId)为“ F001”,则应该仅对数据F001做出响应。但是我得到的是响应中指定的所有数据的响应。我可以知道如何过滤不需要的数据。
#%RAML 1.0
baseUri: https://mocksvc.mulesoft.com/mocks/9b7a0390-ecf4-4ff4-b307-0b7d87ed9495 # baseUri: https://mocksvc.mulesoft.com/mocks/b133e2e4-f0e3-49a0-b224-8f36358e04ca #
title: FlightApi-Rajesh
version: 1.0.1
/{flightId}:
description: Flight Id
get:
queryParameters:
flightId:
displayName: flightId
type: string
required: true
description: Flight name with its ID
example: F0001
description: Get the flight with `flightId = {flightId}`
responses:
200:
body:
application/json:
example: |
{
"F001":{
"flightName": "Ingido",
"Location": "Mumbai",
"flightId": "F001",
"Destination":"Delhi",
"timing":"19:55 HRS"
},
"F002":{
"flightName": "SpiceJet",
"Location": "Pune",
"flightId": "PNQ012",
"Destination":"Chennai",
"timing":"15:00 HRS"
}
}
404:
body:
application/json:
example: |
{"message": "Flight not found"}
答案 0 :(得分:1)
RAML是REST API的设计规范。当您基于RAML运行模拟服务时,它不具有生成动态响应的能力,而这正是您要尝试做的。请参阅Mule论坛以确认这一点-https://forums.mulesoft.com/questions/60487/can-the-example-in-the-raml-be-dynamic.html
如果您的要求是创建具有动态响应的模拟服务,那么还有其他实现此类模拟服务的方法-SOAPUI,Dynatrace等。
如果您的要求是使用RAML并尝试动态响应,则必须基于RAML创建一个Mule ESB项目。然后,您可以基于Mule配置收到的输入来实现动态响应。
答案 1 :(得分:0)
您可能希望将示例更改为此。模拟端点每次都会返回您的示例响应,因此,如果您仅期望一个匹配项,则示例应反映出这一点。请记住这一点,因为您将无法基于用户输入来修改模拟API的响应:
/{flightId}:
description: Flight Id
get:
queryParameters:
flightId:
displayName: flightId
type: string
required: true
description: Flight name with its ID
example: F0001
responses:
200:
body:
application/json:
example: |
{
"F001":{
"flightName": "Ingido",
"Location": "Mumbai",
"flightId": "F001",
"Destination":"Delhi",
"timing":"19:55 HRS"
}
}
404:
body:
application/json:
example: |
{"message": "Flight not found"}
如果您从数据库连接器收到了此数据,即使返回了一条记录,您也会获得结果列表。您需要像这样转换它:
%dw 1.0
%output application/json
%vars flight = (payload filter $.flightId == flowVars.flightId)[0]
---
{
(flowVars.flightId): flight
}