amazon-web-services - 如何在cloudformation中为api网关自定义响应

标签 amazon-web-services templates aws-api-gateway aws-cloudformation

我在 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/

相关文章:

apache - Hadoop/map-reduce : Total time spent by all maps in occupied slots vs. 所有map任务花费的总时间

c++ - 限制 C++ 中值类型的范围

amazon-web-services - 限制 lambda 时,AWS Lambda + API-gateway 返回 502 错误

amazon-web-services - 多账户 API 网关与不同 VPC 中的私有(private) API 网关集成

python - 从API网关获取查询字符串参数

amazon-web-services - 如何导出 CloudWatch Dashboard 数据以与没有 AWS 账户的人共享。

amazon-web-services - 使用 Github 操作在 AWS ECS 上使用 Docker Compose 进行部署

c++ - 模板的数据类型

c++ - 元组处理中的模板函数错误

node.js - 获取API网关传递的Lambda(Nodejs)中的url参数