我的功能通过代理API Gateaway调用。该函数执行OK,但API调用返回502错误:
Mon Dec 30 18:16:25 UTC 2019 : Endpoint response body before transformations: "{}"
Mon Dec 30 18:16:25 UTC 2019 : Execution failed due to configuration error: Malformed Lambda proxy response
Mon Dec 30 18:16:25 UTC 2019 : Method completed with status: 502
响应应为字符串化的JSON,并应包含statusCode,正文和标头。所有这些都包含在下面,但是我仍然遇到由于配置错误而导致执行失败:Lambda代理响应格式错误。帮助吗?
package example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
/**
*
* @author LEVALLOIS
*/
public class Hello implements RequestHandler<Object, String> {
public static void main(String[] args) {
String input = "{\"key\":\"value\"}";
new Hello().handleRequest(input, null);
}
@Override
public String handleRequest(Object input, Context cntxt) {
System.out.println("in the lambda");
String data = input != null ? input.toString() : "{}";
System.out.println("data is: " + data);
JsonObjectBuilder job1 = Json.createObjectBuilder();
job1.add("isBase64Encoded", false);
job1.add("statusCode", 200);
JsonObjectBuilder job2 = Json.createObjectBuilder();
JsonObjectBuilder job3 = Json.createObjectBuilder();
job3.add("key", "value");
job2.add("Content-Type", "application/json");
job1.add("headers", job2.build());
job1.add("body", job3.build().toString());
System.out.println(job1.build().toString());
return job1.build().toString();
}
}
答案 0 :(得分:1)
对不起,我无法回答问题。我来这里是为了设法找到几乎相同的问题的答案。希望以下内容对您有所帮助...
我相信输出应该是带双引号的有效JSon String等?像这样,我在另一个Stack Overflow线程中找到了一个有效的例子?
{"statusCode":200,"headers":{"Content-Type":"application/json"},"multiValueHeaders":null,"body":"{\"msg\":\"Welcome to Belarus! :)\"}","isBase64Encoded":false}
但是当我尝试返回此字符串时,它仍然会导致502错误。我还尝试了上面没有双引号的主体结构和4个反斜杠的方法,这对我来说似乎是错误的,但这没有帮助。以及该字符串的许多其他变体,但我总是收到502错误。
我确实注意到,当我从API网关进行测试时,输出显示
Sat Jan 11 22:53:42 UTC 2020 : Received response. Status: 200, Integration latency: 35 ms
Sat Jan 11 22:53:42 UTC 2020 : Endpoint response headers: {Date=Sat, 11 Jan 2020 22:53:42 GMT, Content-Type=application/json, Content-Length=54, Connection=keep-alive, x-amzn-RequestId=9...}
Sat Jan 11 22:53:42 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"body\":\"Welcome to xxx! :)\"}"
Sat Jan 11 22:53:42 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Sat Jan 11 22:53:42 UTC 2020 : Method completed with status: 502
即我的响应消息很好,但是随后发生了一些事情(“方法响应”选项卡?我很确定我从未更改过,而工作示例未提及)随后尝试处理JSon并失败,因此更改了statusCode到502。但是我不知道为什么为什么失败。
Json包含什么内容?
答案 1 :(得分:1)
尝试使用 lambda-events 中的 APIGatewayProxyResponseEvent
或创建您自己的类。
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>2.2.9</version>
</dependency>
内部处理程序:
public class MyHandler implements RequestHandler<Map<String, Object>, APIGatewayProxyResponseEvent> {
@Override
public APIGatewayProxyResponseEvent handleRequest(Map<String, Object> event, Context context) {
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody("")
.withIsBase64Encoded(false);
}
}
答案 2 :(得分:0)
在API网关控制台中进行更改后,我忘记了“重新部署”。所以也许我的lambda响应中的json出现问题,但是更有可能我在API网关设置中做了一些更改,这些更改未考虑在内,因为我没有重新部署用于说明这些更改的API。