gpt4 book ai didi

java - 由于配置错误 : Malformed Lambda proxy response in Java 导致执行失败

转载 作者:行者123 更新时间:2023-12-05 01:13:51 24 4
gpt4 key购买 nike

我的函数是通过代理 API Gateaway 调用的。函数执行正常,但 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();
}

}

最佳答案

尝试使用来自 lambda 事件的 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);
}

}

关于java - 由于配置错误 : Malformed Lambda proxy response in Java 导致执行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59535245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com