执行“Hello World"时出错适用于 Java 中的 AWS Lambda [英] Error executing "Hello World" for AWS Lambda in Java
问题描述
我编写了以下 Hello World Lambda,我通过 AWS Eclipse 工具包上传到 AWS 来执行它.
I have written following Hello World Lambda which I am executing by uploading on AWS via AWS toolkit for Eclipse.
public class HelloWorldLambdaHandler implements RequestHandler<String, String> {
public String handleRequest(String input, Context context) {
System.out.println("Hello World! executed with input: " + input);
return input;
}
}
执行上述代码时出现以下错误.知道我在这里做错了什么吗?顺便说一句,Maven 项目有这个处理程序,没有任何其他类,唯一的依赖是 aws-lambda-java-core 版本 1.1.0.
I am getting following error when executing above code. Any idea what I maybe doing wrong here? BTW Maven project which have this handler, doesn't have any other class and only dependency is aws-lambda-java-core version 1.1.0.
Skip uploading function code since no local change is found...
Invoking function...
==================== FUNCTION OUTPUT ====================
{"errorMessage":"An error occurred during JSON parsing","errorType":"java.lang.RuntimeException","stackTrace":[],"cause":{"errorMessage":"com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: lambdainternal.util.NativeMemoryAsInputStream@2f7c7260; line: 1, column: 1]","errorType":"java.io.UncheckedIOException","stackTrace":[],"cause":{"errorMessage":"Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: lambdainternal.util.NativeMemoryAsInputStream@2f7c7260; 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.deser.std.StringDeserializer.deserialize(StringDeserializer.java:59)","com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:12)","com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1441)","com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1047)"]}}}
推荐答案
出于某种原因,亚马逊无法将 json 反序列化为字符串.您可能会认为 String 与输入参数一样通用,但无论对错,它都不兼容.
For some reason Amazon can't deserialize json to a String. You would think String would be as general as input parameter as you can get but rightly or wrongly it's not compatible.
要处理 JSON,您可以使用 Map 或自定义 POJO.
To handle JSON you can either use a Map or a custom POJO.
public class HelloWorldLambdaHandler {
public String handleRequest(Map<String,Object> input, Context context) {
System.out.println(input);
return "Hello";
}
}
这篇关于执行“Hello World"时出错适用于 Java 中的 AWS Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!