使用Java和AWS Lambda函数解析JSON错误

时间:2017-08-11 11:12:48

标签: java json amazon-web-services lambda

我怀疑这可能隐藏了另一个问题,但不确定它是什么 - 我有一个小型Lambda函数,我正在尝试使用AWS控制台进行测试。

    public class ApplicationRunner implements RequestHandler<String, String> {

    static final Logger logger = Logger.getLogger(ApplicationRunner.class);

    @Override
    public String handleRequest(String input, Context context) {
    //code

}
}

我收到的错误是:

    {
  "errorMessage": "An error occurred during JSON parsing",
  "errorType": "java.lang.RuntimeException",
  "stackTrace": [],
  "cause": {
    "errorMessage": "com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@6cc4c815; line: 1, column: 1]",
    "errorType": "java.io.UncheckedIOException",
    "stackTrace": [],
    "cause": {
      "errorMessage": "Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@6cc4c815; line: 1, column: 1]",
      "errorType": "com.fasterxml.jackson.databind.JsonMappingException",
      "stackTrace": [
        "com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)",
        "com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857)",
        "com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:62)",
        "com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)",
        "com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1511)",
        "com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1102)"
      ]
    }
  }
}

有人可以帮忙吗?

感谢。

1 个答案:

答案 0 :(得分:1)

你有:

  • <url>/?page=0&size=20&sort=id,desc&sort=name:发送到lambda函数的数据
  • rawData:应用转化模板后的数据
  • rewrittenDatajsonData
  • 的json解析结果

签名rewriteData表示您的handleRequest(String input, Context context)input,此JSON对象为字符串。你把json-string传递给你的lambda? 99.99%不是,你发送的是一个对象,所以你得到了预期的错误信息。

您想要的是扩展类jsonData并使用签名覆盖该函数:

RequestStreamHandler

现在@Override public void handleRequest(final InputStream input, final OutputStream output, final Context context) 是您的input,很可能与rewrittenData相同。