Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

533
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda