Home > other >  Convert JSON to Java object SQSEvent
Convert JSON to Java object SQSEvent

Time:10-21

I need to test a method that parses SQS Events, I'd like to be able to convert JSON to SQSEvent to achieve this.

When I tried ObjectMapper, I'm getting Cannot resolve overloaded method 'readValue' on tmp.readValue(json, SQSEvent)

Documentation on SQSEvent class: https://javadoc.io/static/com.amazonaws/aws-lambda-java-events/2.2.2/com/amazonaws/services/lambda/runtime/events/SQSEvent.html#SQSEvent--

  def main(args: String): Unit = {
    val json = "{ \"prop\":\"value\"}"
    val mapper = new ObjectMapper()

    val sqs = {
      val tmp = new ObjectMapper()
      tmp.readValue(json, SQSEvent)
      tmp
    }

    val test = handleRequest(SQSEvent sqs) //JSON Convert to SQS Event
  }

CodePudding user response:

Make sure to pass the SQSEvent class object to readValue. Then things should resolve fine:

  def main(args: String): Unit = {
    val json = "{ \"prop\":\"value\"}"
    val mapper = new ObjectMapper()

    val sqs = mapper.readValue(json, classOf[SQSEvent])

    val test = handleRequest(sqs: SQSEvent) //JSON Convert to SQS Event
  }
  • Related