python – 使用Inner类输入类型调用AWS lambda函数

我像这样定义了我的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);
}

请求类定义(Inner 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类定义存在问题,它基本上定义了一个非序列化JSON的接收者“”“{”rawValues“:[”jero“],”replaceable“:{”greeting“ :“世界”}}“”“.在documentation中,声明支持Map java类型作为输入类型,但我无法使其工作.你知道将JSON字典映射到Java Maps的java函数的任何例子吗?

解决方法:

不能从外部类的实例外部构造非静态内部类.

尝试使内部类(Request)静态,如:

public static class Request {
    ...
}
上一篇:python – 是boto3.Bucket.upload_file阻塞还是非阻塞?


下一篇:python – Boto AWS Rekognition中的错误