

# 在 OpenAPI 中设置网关响应自定义
<a name="set-up-gateway-responses-in-swagger"></a>

 在 OpenAPI 中，您可以在 API 根级别使用 `x-amazon-apigateway-gateway-responses` 扩展来自定义网关响应。下面的 OpenAPI 定义显示了自定义 `MISSING_AUTHENTICATION_TOKEN` 类型的 [GatewayResponse](https://docs.aws.amazon.com/apigateway/latest/api/API_GatewayResponse.html) 的示例。

```
  "x-amazon-apigateway-gateway-responses": {
    "MISSING_AUTHENTICATION_TOKEN": {
      "statusCode": 404,
      "responseParameters": {
        "gatewayresponse.header.x-request-path": "method.input.params.petId",
        "gatewayresponse.header.x-request-query": "method.input.params.q",
        "gatewayresponse.header.Access-Control-Allow-Origin": "'a.b.c'",
        "gatewayresponse.header.x-request-header": "method.input.params.Accept"
      },
      "responseTemplates": {
        "application/json": "{\n     \"message\": $context.error.messageString,\n     \"type\":  \"$context.error.responseType\",\n     \"stage\":  \"$context.stage\",\n     \"resourcePath\":  \"$context.resourcePath\",\n     \"stageVariables.a\":  \"$stageVariables.a\",\n     \"statusCode\": \"'404'\"\n}"
      }
    }
```

在此示例中，自定义设置将状态代码从默认 (`403`) 更改为 `404`。此外，它还会为网关响应添加了四个标头参数和一个 `application/json` 媒体类型的正文映射模板。