gerosalesc
gerosalesc

Reputation: 3063

Calling AWS lambda function with Inner class input type

I defined my lambda function like this and uploaded it to Amazon:

@Override
public String handleRequest(Request input, Context context) {
    String template = "%s ${greeting}";
    StrSubstitutor ss = new StrSubstitutor(input.replaceables);
    String inputted = String.format(template, input.rawValues[0]);
    return ss.replace(inputted);
}

The request class definition (Inner class):

public class Request {
    String[] rawValues;
    HashMap<String, String> replaceables;

    public Request(){
        replaceables = new HashMap<String, String>();
    }

    //...getter and setters
}

Then call it from boto3 and get this answer:

>>> client = boto3.client('lambda', 'us-west-2')
>>> resp = client.invoke(FunctionName='myfunction', InvocationType='RequestResponse', Payload="""{"rawValues": ["jero"], "replaceables": {"greeting": "world"}}""")
>>> resp['Payload'].read()
'{"errorMessage":"An error occurred during JSON parsing","errorType":"java.lang.RuntimeException","stackTrace":[],"cause":{"errorMessage":"com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class mypackage.functions.Demo$Request]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)\\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@735b478; line: 1, column: 2]","errorType":"java.io.UncheckedIOException","stackTrace":[],"cause":{"errorMessage":"No suitable constructor found for type [simple type, class mypackage.functions.Demo$Request]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)\\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@735b478; line: 1, column: 2]","errorType":"com.fasterxml.jackson.databind.JsonMappingException","stackTrace":["com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)","com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1080)","com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)","com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:142)","com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1441)","com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1047)"]}}}'

Complete Error as shown when testing in amazon console:

"cause": {
  "errorMessage": "No suitable constructor found for type [simple type, class mypackage.functions.Demo$Request]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@2c9f9fb0; line: 1, column: 4]",
  "errorType": "com.fasterxml.jackson.databind.JsonMappingException",
  "stackTrace": [
    "com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)",
    "com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1080)",
    "com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)",
    "com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:142)",
    "com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1441)",
    "com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1047)"
  ]
}

So the main error returned is making me think that there is a problem with my Request class definition which basically defines the recipient for an unserialized JSON """{"rawValues": ["jero"], "replaceable": {"greeting": "world"}}""". In the documentation, it is stated that Map java type is supported as input type but I can not get this working. Do you know o any example of a java function mapping JSON dictionaries to Java Maps?

Upvotes: 5

Views: 1563

Answers (1)

ComputerDruid
ComputerDruid

Reputation: 17586

Non-static inner classes can't be constructed from outside an instance of the outer class.

Try making the inner class (Request) static, like:

public static class Request {
    ...
}

Upvotes: 4

Related Questions