从Node.js调用Java Lambda函数

时间:2019-04-15 15:47:44

标签: java node.js json amazon-web-services aws-lambda

我有一个用Java编写的Lambda函数,我想从NodeJS调用它。这可能吗?我收到以下错误:

An error occurred during JSON parsing: java.lang.RuntimeException
java.lang.RuntimeException: An error occurred during JSON parsing
Caused by: java.io.UncheckedIOException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: lambdainternal.util.NativeMemoryAsInputStream@5dc0ff7d; line: 1, column: 1]
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: lambdainternal.util.NativeMemoryAsInputStream@5dc0ff7d; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:62)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1511)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1102)

这是我的Lambda的代码:

public String handleRequest(String input, Context context) {
  context.getLogger().log("input: " + input + "\n");
  JSONObject obj = new JSONObject(input);
  String dest_key = obj.getString("key");
  context.getLogger().log("key: " + dest_key + "\n");

  ...
}

这是我的JavaScript调用上面的Lambda:

const AWS = require('aws-sdk');

const payload = "{\"key\": \"slide.pptx\"}"

AWS.config.loadFromPath('./config.json');
const lambda = new AWS.Lambda({ region: "ap-south-1" });
const params = {
  FunctionName : 'slide-builder',
  InvocationType : 'RequestResponse',
  Payload: payload // I get the same error even without a payload
};

lambda.invoke(params, function(err, data) {
  if (err) console.log(err, err.stack);
  else console.log(JSON.stringify(data));
});

2 个答案:

答案 0 :(得分:1)

您有两个选择会比您现有的更好。尝试获取String从来没有用过,因为Java Lambda会在获取JSON有效负载之前先对其进行解释。

一种选择是消除试图对输入对象进行任何解释的Lambda。像这样:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public void handleRequest(InputStream inputStream,
                          OutputStream outputStream,
                          Context context) throws IOException {

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonNode = objectMapper.readTree(inputStream);
    String key= jsonNode.get("key").asText();
    // key will now be "slide.pptx"

    // create object to return...
    MyObject myObject = new MyObject();

    // create JSON string
    String jsonReturn = objectMapper.writeValueAsString(myObject);

    // "return" the string
    outputStream.write(jsonReturn .getBytes(Charset.forName("UTF-8")));
}

当Java Lambda尝试使传入的数据进入所需的对象时,您要做的事情避免了Java Lambda的“帮助”。当您有一个完整的对象但在这里不执行您想要的操作时,效果很好。

另一种选择是创建一个镜像您的Node对象的对象:

public class KeyObject {
    private String key;

    public String getKey() {
        return key;
    }
}

,然后将您的处理函数设为:

public String handleRequest(KeyObject key, Context context) {

    String fileName = key.getKey();

    // return as you're doing now.
}

我相信这些都能满足您的需求。

答案 1 :(得分:0)

(From this answer)

L = ['A','B','C','D']
for i, x in enumerate(L):
  print(i,x)

我对stdunbar的解决方案不太满意,但这对我有用。