【问题标题】:JacksonSerializer doesn't work in Kotlin LambdaJacksonSerializer 在 Kotlin Lambda 中不起作用
【发布时间】:2019-04-30 23:12:19
【问题描述】:

当我使用JacksonSerializer() 功能创建客户端并进行一些 API 调用,然后在我的本地计算机上运行该脚本时,我没有收到任何错误并且脚本运行成功。但是,当我将此脚本作为 AWS Lambda 上传时,我收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of kotlin.coroutines.Continuation, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information

起初,我以为错误源于我在 Handler 类之外构建客户端,但是当我在 Handler 类中将客户端设置为私有值时,我仍然得到错误。我在我的函数中包含了println() 语句,但它们甚至没有运行。这告诉我我的handleRequest() 功能没有运行。是否有一些 AWS/Lambda'ism 阻止我使用 JacksonSerializer() 功能?如果是这样,是否有其他方法可以使用 Ktor 客户端解析 JSON 响应?

我的客户端构造:

private val client = HttpClient(Apache) {
    install(JsonFeature) {
        serializer = JacksonSerializer()
    }
}

使用客户端的示例调用:

val response = client.post<JsonNode> {
    url(URL(GITHUB_GRAPHQL_ENDPOINT))
    body = reqBody
    headers {
        append("Authorization", "bearer $token")
    }
}

【问题讨论】:

    标签: kotlin jackson aws-lambda


    【解决方案1】:

    我猜你让你的处理函数成为一个 kotlin suspend 函数?如果是这样,那是你的问题。

    当你标记一个函数suspend 时,编译器会应用一大堆魔法。大多数时候,你不需要知道任何关于这个的事情,除了任何suspend 函数都会在其签名中添加一个kotlin.coroutines.Continuation 类型的额外参数。您通常不会注意到这一点,因为编译器还会使函数调用传递它们自己隐藏的 Continuation 参数。

    Continuation,按照设计,不能由像 Jackson 这样的工具创建 - 这是一个内部的东西。您可能需要做的(假设您确实使处理程序函数suspend)是将您的方法包装在runBlocking {} 中,并使其不是suspend 函数。创建一个新的处理程序可能是最简单的,如下所示:

    fun fixedHandler(input: MyInput, context: Context) = runBlocking {
      originalHandler(input, context)
    }
    
    
    suspend fun originalHandler(input: MyInput, context: Context): MyOutput {
      TODO("This is your original code")
    }
    

    PS - 我通常发现最好利用预定义的 Lambda 接口来编写我的 Lambda 函数 - 它可以防止您遇到此类问题。请参阅https://docs.aws.amazon.com/lambda/latest/dg/java-handler-using-predefined-interfaces.html 了解如何操作。

    【讨论】:

      【解决方案2】:

      您是否检查过在本地运行的依赖项与在 AWS 中运行的依赖项?我在本地运行某个版本时遇到了问题,但 AWS 中的版本不同。这可以特别解释关于延续的错误......也许方法签名在您使用的任何版本中都不同?

      在你的 gradle/maven pom 中特别寻找 provided 范围。这些地方很容易导致版本不同步。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-23
        • 1970-01-01
        • 1970-01-01
        • 2021-05-22
        • 2019-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多