无法使用InvokeRequest.withPayload()方法将字符串传递给AWS Lambda

时间:2019-08-20 04:22:45

标签: json amazon-web-services aws-lambda

我有一个lambda函数,该函数接受String作为输入参数。运行lambda函数时,出现以下错误: Can not deserialize instance of java.lang.String out of START_OBJECT token\n

这也是我的代码所称的样子:

InvokeRequest request = new InvokeRequest();
final String payload = "";
request.withFunctionName(FUNCTION_NAME).withPayload((String) null);
InvokeResult invokeResult = lambdaClient.invoke(request);
Assert.assertEquals(new String (invokeResult.getPayload().array(), "UTF-8"), "Success");

这就是我的处理程序的样子:

public String handleRequest(String s, Context context) {}

现在字符串的内容不再重要,它可以为null,也可以为任何值。我不使用输入。显而易见的解决方案是将其删除,但是由于使用了注释生成器,所以我无法做到这一点。我尝试了ByteBuffer输入,字符串,空字符串,JSON字符串{\"s\":\"s\"},但似乎没有任何效果。我相信我需要传递一个字符串(即否{})。但是由于我正在使用InvokeRequest,所以我不相信我可以做到。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

它通过传递JSON有效字符串来工作。

String payload = "{ \"subject\" : \"content\"}";
request.withFunctionName(functionName)           
                .withPayload(payload);
  

如果需要的话,在接收端必须再次将其从Object映射到String。在这里,我为此使用了Jackson ObjectMapper。

ObjectMapper objectMapper = new ObjectMapper();
        try {
            String payload = objectMapper.writeValueAsString(input);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }