使用内部 class 输入类型调用 AWS lambda 函数

Calling AWS lambda function with Inner class input type

我这样定义 lambda 函数 并将其上传到亚马逊:

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

请求class定义(内class):

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

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

    //...getter and setters
}

然后从 boto3 调用它并得到这个答案:

>>> 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)"]}}}'

在亚马逊控制台中测试时显示的完整错误:

"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)"
  ]
}

所以返回的主要错误让我认为我的 Request class 定义有问题,它基本上定义了未序列化的接收者 JSON """{"rawValues": ["jero"], "replaceable": {"greeting": "world"}}"""。在 documentation 中,声明支持 Map java 类型作为输入类型,但我无法使其正常工作。您知道 java 函数将 JSON 词典映射到 Java 地图的任何示例吗?

非静态内部 classes 不能从外部 class.

的实例外部构造

尝试使内部 class (Request) 静态化,例如:

public static class Request {
    ...
}