我发现了新信息。我想我可能为 S3 事件源实现了不正确的接口(interface)。到目前为止,我还无法让它工作或理解应该实现哪个接口(interface)(如果有)。 RequestHandler 接口(interface)需要方法handleRequest 返回一个值。有什么建议么?
outputType – If you plan to invoke the Lambda function synchronously (using the RequestResponse invocation type), you can return the output of your function using any of the supported data types. For example, if you use a Lambda function as a mobile application backend, you are invoking it synchronously. Your output data type will be serialized into JSON.
If you plan to invoke the Lambda function asynchronously (using the Event invocation type), the outputType should be void. For example, if you use AWS Lambda with event sources such as Amazon S3, Amazon Kinesis, and Amazon SNS, these event sources invoke the Lambda function using the Event invocation type.
我已经创建了一个基本的处理程序,但似乎错过了一个继续暗示我的关键步骤。
代码和错误如下:
package example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.model.S3Event;
public class Hello implements RequestHandler<S3Event, Object> {
public String handleRequest(S3Event input, Context context) {
return "Hello";
}
}
{ "errorMessage": "An error occurred during JSON parsing",
"errorType": "java.lang.RuntimeException", "stackTrace": [],
"cause": { "errorMessage": "com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.amazonaws.services.s3.model.S3Event out of START_OBJECT token\n at Source: lambdainternal.util.NativeMemoryAsInputStream@566776ad; line: 1, column: 1", "errorType": "java.io.UncheckedIOException", "stackTrace": [], "cause": { "errorMessage": "Can not deserialize instance of com.amazonaws.services.s3.model.S3Event out of START_OBJECT token\n at Source: lambdainternal.util.NativeMemoryAsInputStream@566776ad; line: 1, column: 1", "errorType": "com.fasterxml.jackson.databind.JsonMappingException", "stackTrace": [ "com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)", "com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835)", "com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:831)", "com.fasterxml.jackson.databind.deser.std.EnumDeserializer._deserializeOther(EnumDeserializer.java:137)", "com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:89)", "com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:18)", "com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1441)", "com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1047)"] } } }
最佳答案
检查您是否导入了正确的 S3Event 类,即 com.amazonaws.services.lambda.runtime.events.S3Event。
我遇到了同样的异常,发现我已导入 com.amazonaws.services.s3.model.S3Event。
关于java - AWS Lambda Java 处理程序 Hello World,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36631725/