Search code examples
javaaws-lambdaaws-sdkalexa-skills-kit

Parsing the input in a AWS Lambda handleRequest into a POJO


I am developing an Alexa Skill and I have some problems in the Lambda (written in Java) when parsing the input event.

My Lamba class is like this

public class AWSLambdaLaunch implements RequestHandler<RequestEnvelope, ResponseEnvelope>{

  @Override
  public ResponseEnvelope handleRequest(RequestEnvelope requestEnvelope, Context context) {
    //...
  }
}

But I am getting the following error

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.amazon.ask.model.RequestEnvelope]: 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@543c6f6d; line: 1, column: 2]

No suitable constructor found for type [simple type, class com.amazon.ask.model.RequestEnvelope]: 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@543c6f6d; line: 1, column: 2]

So I think the problem is there is no way to parse the input object into the RequestEnvelope Object from com.amazon.ask.model.RequestEnvelope.

I have also try the com.amazon.ask.dispatcher.request.handler.HandlerInput with the same result.

If I change the

public class AWSLambdaLaunch implements RequestHandler<RequestEnvelope, ResponseEnvelope>

declaration with this one

public class AWSLambdaLaunch implements RequestHandler<Map<String, Object>, ResponseEnvelope>

I get no errors and it works OK, but I find it more "dirty" to retrieve the elements and values from nested HashMaps

So I am wondering if there is a way to parse the input into a POJO directly or there is an already builded POJO which I can use.

[edit] Here is the code for the RequestEnvelope class, from the java aws sdk. As @apandey846 has noted, there is no default constructor in the class, so maybe that is the problem. Is there any way I can use it?

[edit] I have already check that Fully qualified class name for the handler is correctly configured in the Lambda configuration tab as stated here alexa-java-sdk-issue-102. I think issue-104 is about another issue but referenced just because of the template.


Solution

  • It was all my fault. I have been mixing up documentation so I was trying the wrong method. I have followed only this, and now I can have an Amazon POJO initialized.

    To sum up, I was using a lambda handlerequest instead of a skill handle. com.amazon.ask.dispatcher.request.handler.HandlerInput