默认 API Gateway 响应采用以下形式:{"message":$context.error.messageString}
。
通过控制台更改此模板,例如将其包装在 "error"
对象中,效果很好。但是,当我在 CDK 中尝试它时,在部署过程中出现错误,指定了无效的映射表达式
代码(注意,这实际上是默认模板):
webServiceApi.addGatewayResponse('4XX', {
type: apigw.ResponseType.DEFAULT_4XX,
responseHeaders: COMMON_HEADERS,
templates: {
'application/json': '{"message":$context.error.messageString}',
},
});
任何更改都会发生这种情况,包括我能找到的所有 AWS 编写的示例。 AWS 中的各个地方都没有充分记录:CDK、SDK、API Gateway 和 CloudFormation,因此我很难找出那里出了什么问题?
CloudFormation 错误
Resource handler returned message: "Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: *] (Service: ApiGateway, Status Code: 400, Request ID: [TRUNCATED], Extended Request ID: null)" (RequestToken: [TRUNCATED], HandlerErrorCode: InvalidRequest)
最佳答案
显然,错误不在于映射模板,而是源的 "'*'"
所需的 header ,而不是 "*"
。这不是一个有用的错误消息
关于amazon-web-services - 设置 GatewayResponse 时指定的映射表达式无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71900512/