gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:59:13 26 4
gpt4 key购买 nike

我这样定义了我的 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 map 的 Java 函数示例吗?

最佳答案

非静态内部类不能从外部类的实异常(exception)部构造。

尝试将内部类 (Request) 设为静态,例如:

public static class Request {
...
}

关于python - 使用内部类输入类型调用 AWS lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32059090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com