Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

535
Visualizações
How do you register the KotlinModule to the AWS lambda Jackson Object Mapper?

I'm using Kotlin to write an AWS Lambda. I have a Kotlin data class

class MessageObject(
  val id: String,
  val name: String,
  val otherId: String
)

This data class is used as the input to the required interface implementation

class Handler : RequestHandler<MessageObject, Output> {
  ...  
  override fun handleRequest(msg: MessageObject, ctx: Context) {
    ...
  }
}

When I test this lambda in the aws console, and pass it a proper JSON message, I get this:

An error occurred during JSON parsing: java.lang.RuntimeException
java.lang.RuntimeException: An error occurred during JSON parsing
Caused by: java.io.UncheckedIOException: 
com.amazonaws.lambda.thirdparty.com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of 'com.mycode.MessageObject'(no Creators, like default construct, exist): 
cannot deserialize from Object value (no delegate- or property-based Creator)

I'm almost certain this is fixed by saying: ObjectMapper().registerModule(KotlinModule()) but in the world of AWS Lambda how do I edit the object mapper provided by AWS?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you haven't gotten it to work with KotlinModule, since the problem you're having is that Jackson requires a default empty constructor and you currently don't have one. You could just change your MessageObject as follows and it should work:

  data class MessageObject(
  var id: String = "",
  var name: String = "",
  var otherId: String = ""
)

I created this repo with a fully functional kotlin lambda template using the Serverless Framework. Have a look for some other tidbits you might need: https://github.com/crafton/sls-aws-lambda-kotlin-gradlekt

over 4 years ago · Santiago Trujillo Relatório

0

You cannot use data class with provided RequestHandler<I, O> unfortunately, because you need register the kotlin module for your jackson mapper in order to work with data classes. But you can write you own RequestHandler, which will like this one.

Here's the code:

interface MyRequestStreamHandler<I : Any, O : Any?> : RequestStreamHandler {
    val inputType: Class<I>

    fun handleRequest(input: I, context: Context): O?

    override fun handleRequest(inputStream: InputStream, outputStream: OutputStream, context: Context) {
        handleRequest(inputStream.readJson(inputType), context).writeJsonNullable(outputStream)
    }

    interface MessageObjectRequestHandler : MyRequestStreamHandler< MessageObject, Output> {
        override val inputType: Class<MessageObject >
            get() = MessageObject::class.java
    }
}

And jackson util:

private val objectMapper = jacksonObjectMapper()
        .configure(JsonParser.Feature.ALLOW_COMMENTS, true)
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .registerKotlinModule()

private val writer: ObjectWriter = objectMapper.writer()

fun <T : Any> readJson(clazz: Class<T>, stream: InputStream): T =
        objectMapper.readValue(stream, clazz)

fun <T : Any> InputStream.readJson(clazz: Class<T>): T =
        readJson(clazz, this)

fun Any?.writeJsonNullable(outputStream: OutputStream) {
    if (this != null)  writer.writeValue(outputStream, this)
}

Now, you can keep your MessageObject class to be data class, and your handler will look something like:

class LambdaMain : MessageObjectRequestHandler {
    override fun handleRequest(input: MessageObject, context: Context): Output { 
        //...
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda