How to issue a parsing json from case class using jackson

+1 vote

I am attempting to use the aws lambda seed project received from here

The source code for the generated project is here

I imported the sbt dependencies and built the project using sbt assembly command.

I took the jar file built and uploaded it to lambda directly. For the handler defined for at MyHandler::handler

// handler io.github.yeghishe.MySimpleHander::handler
// input "foo"
object MySimpleHander extends App {
  def handler(name: String, context: Context): String = s"Hello $name"
}

case class Name(val name: String)
case class Greeting(val message: String)

// handler io.github.yeghishe.MyHandler
// input {"name": "Yeghishe"}
class MyHandler extends Handler[Name, Greeting] {
  def handler(name: Name, context: Context): Greeting = {
    logger.info(s"Name is $name")
    Greeting(s"Hello ${name.name}")
  }
}

with the input event:

{
  "name": "Yeghishe"
}

When I call the function, it shows the error:

{
  "errorMessage": "An error occurred during JSON parsing",
  "errorType": "java.lang.RuntimeException",
  "stackTrace": [],
  "cause": {
    "errorMessage": "com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.lendi.hellolambda.Name]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@1dfe2924; line: 1, column: 2]",
    "errorType": "java.io.UncheckedIOException",
    "stackTrace": [],
    "cause": {
      "errorMessage": "No suitable constructor found for type [simple type, class com.lendi.hellolambda.Name]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@1dfe2924; line: 1, column: 2]",
      "errorType": "com.fasterxml.jackson.databind.JsonMappingException",
      "stackTrace": [
        "com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)",
        "com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1106)",
        "com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:296)",
        "com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133)",
        "com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1511)",
        "com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1102)"
      ]
    }
  }
}

Sep 26, 2018 in AWS by bug_seeker
• 15,510 points
3,055 views

1 answer to this question.

0 votes

Do you use jackson-module-scala or any other Scala-aware configuration of Jackson? By default Jackson can't deserialize case classes. So show us your Jackson configuration. The seed project depends on a library that uses the circe library for parsing json from an input stream, it is based on this blog post which makes it much stranger when we have the method issuing a Jackson error.
Maybe you should have set your handler to just com.lendi.hellolambda.MyHandler or com.lendi.hellolambda.MyHandler::handleRequest (which comes from the base Handler) See also Example: Using Stream for Handler Input/Output (Java)
 

answered Sep 26, 2018 by Priyaj
• 58,020 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer

How to delete all the subnets from a VPC using boto3?

You can refer to this question here: https://www.edureka.co/community/32182/unable-to-delete-subnets-in-a-vpc-using-boto3 You ...READ MORE

answered Dec 5, 2018 in AWS by Shuvodip Ghosh
1,827 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP