AWS API Gateway,“集成响应”中的“HTTP 状态正则表达式”无法匹配任何字符串

Vin*_*ang 4 amazon-web-services aws-api-gateway

一些背景信息:我的设置是 browser -> api gateway -> ec2 (using spring mvc).
我想返回 200 以外的 http 状态。在谷歌搜索了很多之后,我知道我在 api 网关上做了一些配置。以下是我的配置:

  1. Method Response,添加http状态400
  2. Integration Response->HTTP status regex,添加.*httpStatus":400.*
  3. 在 ec2(使用 spring mvc)中,我总是返回 http 状态 400 并使用以下 json 作为响应主体(用于测试)

    {
    "errorMessage": "{\"errorType\":\"Bad Request\",\"requestId\":\"12345\",\"httpStatus\":400,\"message\":\"you got错误\"}"
    }

但是,当我测试 api(使用 api 网关中的“测试”按钮)时,它仍然返回 http 状态 200。(我希望它应该是 400)
当我将正则表达式更改为 时.*400.*,它按预期返回 http 状态 400。
当我将正则表达式更改为 时.*httpStatus.*,它返回 http 状态 200。(我希望它应该是 400)

为什么正则表达式不能.*httpStatus":400.*识别我的 json?即使我将正则表达式简化为.*httpStatus.*.
但是正则表达式.*400.*可以识别 int 400 ...

PS:我永远不会忘记在api网关上进行任何更改后部署api

这是日志

Execution log for request test-request  
Wed Jul 13 15:38:22 UTC 2016 : Starting execution for request: test-invoke-request  
Wed Jul 13 15:38:22 UTC 2016 : HTTP Method: POST, Resource Path: /graphql  
Wed Jul 13 15:38:22 UTC 2016 : Method request path: {}  
Wed Jul 13 15:38:22 UTC 2016 : Method request query string: {}  
Wed Jul 13 15:38:22 UTC 2016 : Method request headers: {}  
Wed Jul 13 15:38:22 UTC 2016 : Method request body before transformations: null  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint request URI: http://awseb-e-e-AWSEBLoa-1OSQBKG0O3J75-968940376.ap-northeast-1.elb.amazonaws.com/graphql  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint request headers: {x-amzn-apigateway-api-id=kymhygbqwa, Accept=application/json, User-Agent=AmazonAPIGateway_kymhygbqwa}  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint request body after transformations: null  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint response body before transformations: {"errorMessage":"{\"errorType\":\"Bad Request\",\"requestId\":\"12345\",\"httpStatus\":400,\"message\":\"you got error\"}"}  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint response headers: {date=Wed, 13 Jul 2016 15:38:22 GMT, server=Apache-Coyote/1.1, expires=0, transfer-encoding=chunked, access-control-allow-headers=Origin, X-Requested-With, Content-Type, Accept, x-frame-options=DENY, access-control-allow-methods=POST, GET, PUT, OPTIONS, DELETE, pragma=no-cache, access-control-allow-origin=*, access-control-max-age=3600, x-content-type-options=nosniff, x-xss-protection=1; mode=block, content-type=application/json;charset=UTF-8, connection=keep-alive, cache-control=no-cache, no-store, max-age=0, must-revalidate}  
Wed Jul 13 15:38:22 UTC 2016 : Method response body after transformations: {"errorMessage":"{\"errorType\":\"Bad Request\",\"requestId\":\"12345\",\"httpStatus\":400,\"message\":\"you got error\"}"}  
Wed Jul 13 15:38:22 UTC 2016 : Method response headers: {Content-Type=application/json}  
Wed Jul 13 15:38:22 UTC 2016 : Successfully completed execution  
Wed Jul 13 15:38:22 UTC 2016 : Method completed with status: 200  
Run Code Online (Sandbox Code Playgroud)

Rya*_*anG 7

您缺少一个关键要素:

对于 HTTP 和 AWS 集成,错误模式与集成响应的HTTP 状态代码相匹配。对于 Lambda“调用”集成,针对响应中“errorMessage”字段的错误模式。

您将需要使用来自 SpringMVC 端点的响应状态代码来控制 API 网关响应。

作为参考,这里的第 5.b 节中埋藏着这一点智慧

另请参阅此处的有关错误映射的一些更精细点的博客文章