上次更新日期:2021 年 3 月 22 日
我使用 AWS CloudFormation 模板或 OpenAPI (Swagger) 定义来创建一个具有代理资源的 Amazon API Gateway API。当我试着将 URL 路径参数添加至该 API 时,我收到了错误消息“Invalid mapping expression specified”。如何解决此错误?
简短描述当代理路径参数 {proxy+} 没有定义的 URL 路径参数映射时,API Gateway 会返回一个 Invalid mapping expression specified 错误。
要解决此错误,请执行以下操作:
对于 AWS CloudFormation 模板,定义 AWS::ApiGateway::Method 部分的 RequestParameters 属性。 对于 OpenAPI 定义,定义 x-amazon-apigateway-any-method object 下的 parameters 部分。 解决方法 对于 AWS CloudFormation 模板1.更新 CloudFormation 模板,以便将 RequestParameters 值设置为 true。
示例 CloudFormation 模板 RequestParameters
...
.
.
ProxyMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
.
.
RequestParameters:
method.request.path.proxy: true
Integration:
RequestParameters:
integration.request.path.proxy: 'method.request.path.proxy'
IntegrationHttpMethod: ANY
.
.
...
有关更新 API Gateway 资源的更多信息,请参阅 Amazon API Gateway 资源类型参考。
2.使用 CloudFormation 模板更新您的 API,从而来更新 AWS CloudFormation 堆栈。
对于 OpenAPI 定义1.更新您的 API 定义,使 x-amazon-apigateway-any-method 部分下的 parameters 具有以下值:
"x-amazon-apigateway-any-method": {
"parameters":
....
....
}
有关更多信息,请参阅 OpenAPI 网站上的描述参数。
2.通过将更新后的 API 定义文件导入 API Gateway 来更新 API。
测试设置1.在 API Gateway 控制台中,选择您的 API 的名称。
2.根据使用案例的需要添加 URL 路径参数。
注意:不应出现 Invalid mapping expression specified 错误。