我在 cloudformation 的 apigateway 模板中对我的 GET
方法进行了以下配置
"paths": {
"/customer/{customerid}": {
"get": {
"description": "Returns JSON customer objects from DynamoDB.",
"parameters": [
{
"required": true,
"type": "string",
"name": "customerid",
"in": "path"
}
],
"produces": [
"application/json"
],
"x-amazon-apigateway-integration": {
"passthroughBehavior": "never",
"responses": {
"default": {
"statusCode": "200"
}
},
"uri": {
"Fn::Join": [
":",
[
"arn",
"aws",
"apigateway",
{
"Ref": "AWS::Region"
},
"dynamodb",
"action/GetItem"
]
]
},
"httpMethod": "POST",
"requestTemplates": {
"application/json": "{\n \"TableName\": \"customer\",\n \"Key\": {\n \"customerid\": {\n \"S\": \"$input.params('customerid')\"\n }\n }\n}\n"
},
"credentials": {
"Fn::GetAtt": [
"TableAccessRole",
"Arn"
]
},
"type": "aws"
},
"consumes": [
"application/json"
],
"responses": {
"200": {
"description": "200 response"
}
}
}
}
api 正在完美创建,但是 api 的响应是
{
"Item": {
"Name": {
"S": "Alex"
},
"CustomerId": {
"S": "123"
}
}
}
但我希望这是一个像
这样的简单 json{
"Name":"Alex",
"CustomerId":"123"
}
我正在查看 aws 文档,但我无法弄清楚我的配置的哪一部分需要更改。我知道我有 input
变量,我可以用它来获取数据,但是在哪里以及如何获取数据,我很迷失
最佳答案
由于您现有的模板使用 x-amazon-apigateway-integration
Swagger extension ,您可以添加 responseTemplates
包含您的响应映射模板到现有默认
的对象response
您已经定义了:
"x-amazon-apigateway-integration": {
"passthroughBehavior": "never",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "{\"Name\": \"$input.path('$.Item.Name.S')\", \"CustomerId\": \"$input.path('$.Item.CustomerId.S')\"}"
}
}
},
[...]
关于amazon-web-services - 如何在cloudformation中为api网关自定义响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42075262/