How to convert sample in AWS Lambda from javascript to Java? -
i trying convert javascript sample:
javascript
'use strict'; exports.handler = (event, context, callback) => { callback(null, {"speech": "hello lambda js"}); };
output hitting test button in aws lambda
{ "speech": "hello lambda js" }
now want convert in simplest way java program.
this tried:
public class sample implements requesthandler<string, string> { @override public string handlerequest(string s, context context) { return "{\"speech\": \"hello lambda java\"}"; } }
but hitting test button in aws lamda complains:
{ "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\n @ [source: lambdainternal.util.nativememoryasinputstream@5f375618; line: 1, column: 1]", "errortype": "java.io.uncheckedioexception", "stacktrace": [], "cause": { "errormessage": "can not deserialize instance of java.lang.string out of start_object token\n @ [source: lambdainternal.util.nativememoryasinputstream@5f375618; 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:857)", "com.fasterxml.jackson.databind.deser.std.stringdeserializer.deserialize(stringdeserializer.java:62)", "com.fasterxml.jackson.databind.deser.std.stringdeserializer.deserialize(stringdeserializer.java:11)", "com.fasterxml.jackson.databind.objectreader._bindandclose(objectreader.java:1511)", "com.fasterxml.jackson.databind.objectreader.readvalue(objectreader.java:1102)" ] } } }
your javascript function returning object literal, not json string. need parallel in java, return instance of class contains property named speech, getter , setter speech, , value of speech set "hello lambda java". aws take care of serializing object json response want.
more typing in java javascript. ha! punned.
Comments
Post a Comment