Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

amazon web services - Error executing "Hello World" for AWS Lambda in Java

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;
    }
}

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)"]}}}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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";
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...