Json AWS API网关与Lambda代理集成失败

Json AWS API网关与Lambda代理集成失败,json,scala,amazon-web-services,aws-lambda,aws-api-gateway,Json,Scala,Amazon Web Services,Aws Lambda,Aws Api Gateway,我的问题很简单,真的 我编写了一个Scala处理程序类,该类返回一个包含以下JSON的字符串: { "isBase64Encoded":false, "statusCode":404, "headers":{ "Content-Type":"text/html" }, "body":"<p>The requested resource could not be found.</p>" } 我正在使用AWS Java SDK中的R

我的问题很简单,真的

我编写了一个Scala处理程序类,该类返回一个包含以下JSON的字符串:

{
   "isBase64Encoded":false,
   "statusCode":404,
   "headers":{
      "Content-Type":"text/html"
   },
   "body":"<p>The requested resource could not be found.</p>"
}
我正在使用AWS Java SDK中的
RequestHandler
APIGatewayProxyRequestEvent

但是当调用它时,我得到一个错误
由于配置错误执行失败:Lambda代理响应格式错误

以下是完整的日志:

Execution log for request 99234b33-2419-11e9-8156-d18557467ee6
Tue Jan 29 22:59:57 UTC 2019 : Starting execution for request: 99234b33-2419-11e9-8156-d18557467ee6
Tue Jan 29 22:59:57 UTC 2019 : HTTP Method: GET, Resource Path: /foo
Tue Jan 29 22:59:57 UTC 2019 : Method request path: {proxy=foo}
Tue Jan 29 22:59:57 UTC 2019 : Method request query string: {}
Tue Jan 29 22:59:57 UTC 2019 : Method request headers: {}
Tue Jan 29 22:59:57 UTC 2019 : Method request body before transformations: 
Tue Jan 29 22:59:57 UTC 2019 : Endpoint request URI: https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:794147591978:function:xxxx/invocations
Tue Jan 29 22:59:57 UTC 2019 : Endpoint request headers: {x-amzn-lambda-integration-tag=99234b33-2419-11e9-8156-d18557467ee6, Authorization=***************************************************************************************************************************************************************************************************************************************************************************************************************************e4cddd, X-Amz-Date=20190129T225957Z, x-amzn-apigateway-api-id=0lypbq3och, X-Amz-Source-Arn=arn:aws:execute-api:eu-central-1:794147591978:0lypbq3och/test-invoke-stage/GET/{proxy+}, Accept=application/json, User-Agent=AmazonAPIGateway_0lypbq3och, X-Amz-Security-Token=FQoGZXIvYXdzECAaDNe+xBH2UeEIx4SHoyK9A24kTKoADNrqj2oEsP7wwqlNw/Z3DHCHLqagRvBVke5R7MUg4G4cTFG0KQia0x3x4CqYIjTtQ6oFp2iWuTV8LOuMQz6r4T75Y5wrvl3c2qfkUd7m+Oj2B5Q0TXKtI/DBa0I4ChqAVgamMuPTVbZga0u9gG4d+v7sEzeOrMDrp1AsiBYQMG+S9MbTC/2+UuFe2X7elWnAhSXAZ/LA7w/dQu8+UvPPLN2xVl0W7d/9Nsq0BM1B5OBgJaj5gO9r8zh1Yto96100F3JaIdjLk+btcyy53OnL+SQWaS2isF/2Gl [TRUNCATED]
Tue Jan 29 22:59:57 UTC 2019 : Endpoint request body after transformations: {"resource":"/{proxy+}","path":"/foo","httpMethod":"GET","headers":null,"multiValueHeaders":null,"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":{"proxy":"foo"},"stageVariables":null,"requestContext":{"path":"/{proxy+}","accountId":"794147591978","resourceId":"m044wv","stage":"test-invoke-stage","domainPrefix":"testPrefix","requestId":"99234b33-2419-11e9-8156-d18557467ee6","identity":{"cognitoIdentityPoolId":null,"cognitoIdentityId":null,"apiKey":"test-invoke-api-key","cognitoAuthenticationType":null,"userArn":"arn:aws:iam::794147591978:root","apiKeyId":"test-invoke-api-key-id","userAgent":"aws-internal/3 aws-sdk-java/1.11.481 Linux/4.9.137-0.1.ac.218.74.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.192-b12 java/1.8.0_192","accountId":"794147591978","caller":"794147591978","sourceIp":"test-invoke-source-ip","accessKey":"ASIA3RZXCS4VNQOJVZON","cognitoAuthenticationProvider":null,"user":"794147591978"},"domainName":"testPrefix.tes [TRUNCATED]
Tue Jan 29 22:59:57 UTC 2019 : Sending request to https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:794147591978:function:xxx/invocations
Tue Jan 29 22:59:57 UTC 2019 : Received response. Integration latency: 36 ms
Tue Jan 29 22:59:57 UTC 2019 : Endpoint response body before transformations: "{\"isBase64Encoded\":false,\"statusCode\":404,\"headers\":{\"Content-Type\":\"text/html\"},\"body\":\"<p>The requested resource could not be found.</p>\"}"
Tue Jan 29 22:59:57 UTC 2019 : Endpoint response headers: {Date=Tue, 29 Jan 2019 22:59:57 GMT, Content-Type=application/json, Content-Length=156, Connection=keep-alive, x-amzn-RequestId=bd41f477-92ae-4068-a062-16a149cf7748, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5c50daed-8291ac2e0f2b8a251dbf1865;sampled=0}
Tue Jan 29 22:59:57 UTC 2019 : Execution failed due to configuration error: Malformed Lambda proxy response
Tue Jan 29 22:59:57 UTC 2019 : Method completed with status: 502
请求99234b33-2419-11e9-8156-d18557467ee6的执行日志 2019年1月29日星期二22:59:57 UTC:开始执行请求:99234b33-2419-11e9-8156-d18557467ee6 星期二1月29日22:59:57 UTC 2019:HTTP方法:获取,资源路径:/foo Tue Jan 29 22:59:57 UTC 2019:方法请求路径:{proxy=foo} Tue Jan 29 22:59:57 UTC 2019:方法请求查询字符串:{} Tue Jan 29 22:59:57 UTC 2019:方法请求头:{} Tue Jan 29 22:59:57 UTC 2019:转换前的方法请求正文: 2019年1月29日星期二22:59:57 UTC:端点请求URI:https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:794147591978:函数:xxxx/调用 2019年1月29日星期二22:59:57 UTC:端点请求标头:{x-amzn-lambda-integration-tag=99234b33-2419-11e9-8156-d18557467ee6,授权=***************************************************************************************************************************************************************************************************************************************************************************************************************************e4cddd,X-Amz-Date=20190129T225957Z,X-amzn-apigateway-api-id=0lypbq3och,X-Amz-Source-Arn=Arn:aws:execute-api:eu-central-1:794147591978:0lypbq3och/test invoke stage/GET/{proxy+},Accept=application/json,User Agent=AmazonAPIGateway_0lypbq3och,X-Amz-Security-Token=fqogzxivyxcdzecadane+xb2b2ueeix4shoyk4ktkoadnrqj2oesp7wqlnw/z3dhchlqagrgrvke5r7mug0kqia0x3x4cqyjtqtq2w8loumqf7mqf7m+oj2b5q2b5q2b5q0txkti/dba2b7chqqgamavgamuptzg9g9g4d+zv2xqqu2xqwq2xq2f2f7f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f7f7l0W7d/9Nsq0BM1B5OBgJaj5gO9r8zh1Yto96100F3JaIdjLk+btcyy53OnL+SQWaS2isF/2Gl[截断] Tue Jan 29 22:59:57 UTC 2019:转换后的端点请求正文:{“资源”:“/{proxy+}”、“路径”:“/foo”、“httpMethod”:“获取”、“标头”:null,“多值标头”:null,“queryStringParameters”:null,“多值queryStringParameters”:null,“路径参数”:{“代理”:“foo”},“stageVariables”:null,“requestContext”:{“路径”:“/{proxy+}”、“帐户ID”:“794147591978”,“resourceId:“m044wv”,“stage:“test invoke stage”,“domainPrefix:“testPrefix”,“requestId:“99234b33-2419-11e9-8156-d18557467ee6”,“identity:”{“cognitoIdentityPoolId”:null,“cognitoIdentityId”:null,“apiKey:“test invoke api key”,“cognitoAuthenticationType”:null,“userArn:“arn:aws:iam::794147591978:root”,“apiKeyId:”测试调用api密钥id,“用户代理”:“aws内部/3 aws sdk java/1.11.481 Linux/4.9.137-0.1.ac.218.74.329.metal1.x86\u 64 OpenJDK\u 64位服务器\u VM/25.192-b12 java/1.8.0\u 192”,“帐户id”:“794147591978”,“调用方”:“794147591978”,“源ip”:“测试调用源ip”,“访问密钥”:“ASIA3RZXCS4VNQOVZON”,“cognitoAuthenticationProvider”:null,“用户”:”794147591978“},“域名”:“testPrefix.tes[截断] 2019年1月29日星期二22:59:57 UTC:向发送请求https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:794147591978:函数:xxx/调用 2019年1月29日星期二22:59:57 UTC:收到响应。集成延迟:36毫秒 2019年1月29日星期二22:59:57 UTC:转换之前的端点响应正文:“{\'isBase64Encoded\':false,\'statusCode\':404,\'headers\':{\'Content Type\':\'text/html\',\'body\':\”找不到请求的资源。

\“}” 2019年1月29日星期二22:59:57 UTC:端点响应标题:{Date=Tue,2019年1月29日22:59:57 GMT,内容类型=应用程序/json,内容长度=156,连接=保持活动,x-amzn-RequestId=bd41f477-92ae-4068-a062-16a149cf7748,x-amzn-remap-Content-Length=0,x-Amz-Executed-Version=$LATEST,x-amzn-Trace-Id=root=1-5c50daed-8291ac2e0f2b8a251dbf1865;抽样=0} 2019年1月29日星期二22:59:57 UTC:由于配置错误,执行失败:Lambda代理响应格式错误 2019年1月29日星期二22:59:57 UTC:方法已完成,状态:502
日志中包含的JSON(除了转义之外)似乎与我所期望的完全匹配,并且它也与当时的文档匹配

那么,为什么会出现此错误?

请尝试对正文值执行
JSON.stringify()

{
   "isBase64Encoded":false,
   "statusCode":404,
   "headers":{
      "Content-Type":"text/html"
   },
   "body":JSON.stringify("<p>The requested resource could not be found.</p>")
}
{
“isBase64Encoded”:false,
“状态代码”:404,
“标题”:{
“内容类型”:“文本/html”
},
“body”:JSON.stringify(找不到请求的资源。

) }
我知道这看起来很傻,但在产出上存在差异:

console.log("<p>The requested resource could not be found.</p>");
// "<p>The requested resource could not be found.</p>"

console.log(JSON.stringify("<p>The requested resource could not be found.</p>"));
// "'<p>The requested resource could not be found.</p>'"
console.log(“找不到请求的资源。

”; //“找不到请求的资源。

” log(JSON.stringify(“找不到请求的资源。

”); //““找不到请求的资源。


正如您可能注意到的那样,输出已经是一个有效的JSON。除此之外,
JSON.stringify
是JavaScript,我正在使用JVM。我将用更多的上下文更新我的问题。啊,这是有意义的,我道歉。感谢您提供了额外的上下文。我仍然相信问题在于您的resp中JSON对象的字符串表示onse.
“{\'isBase64Encoded\”:false,\'statusCode\”:404,\'headers\:{\'Content Type\':\'text/html\',\'body\':\“找不到请求的资源。

“}
以同样的方式是有效的JSON
“foo”
是有效的JSON。它直接从服务器传递而不作为对象进行解析。请查看日志中请求正文和响应正文之间的差异。问题是我确认附加转义发生在Lambda执行后的某个地方。。。
console.log("<p>The requested resource could not be found.</p>");
// "<p>The requested resource could not be found.</p>"

console.log(JSON.stringify("<p>The requested resource could not be found.</p>"));
// "'<p>The requested resource could not be found.</p>'"