使用Inner类输入类型

时间:2015-08-17 20:23:47

标签: python amazon-web-services java-8 aws-lambda boto3

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

请求类定义(内部类):

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": "world"}}"""的收件人。在documentation中,声明支持Map java类型作为输入类型,但我无法使其正常工作。你知道将JSON字典映射到Java Maps的java函数的任何例子吗?

1 个答案:

答案 0 :(得分:4)

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

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

public static class Request {
    ...
}