Leo
Leo

Reputation: 92

Jackson JSON deserialization for Amazon Lex AWS Lambda hook

I have a problem with deserialization implemented in AWS Lex Lambda hook. I have an AWS Lambda function to validate the user input, but I keep getting JSONMapping errors. The Lex json is like this:

{
  "currentIntent": {
    "name": "intent-name",
    "slots": {
      "slot-name": "value",
      "slot-name": "value",
      "slot-name": "value"
    },
    "confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)",
  },
  "bot": {
    "name": "bot-name",
    "alias": "bot-alias",
    "version": "bot-version"
  },
  "userId": "User ID specified in the POST request to Amazon Lex.",
  "inputTranscript": "Text used to process the request",
  "invocationSource": "FulfillmentCodeHook or DialogCodeHook",
  "outputDialogMode": "Text or Voice, based on ContentType request header in runtime API request",
  "messageVersion": "1.0",
  "sessionAttributes": { 
     "key1": "value1",
     "key2": "value2"
  }
}

And my Java bean for deserializing this JSON is:

public class RequestInput {
    public class CurrentIntent {
        @JsonProperty("name")
        String name;
        @JsonProperty("slots")
        Map<String, String> slots;
        @JsonProperty("confirmationStatus")
        String confirmationStatus;

        public CurrentIntent(@JsonProperty("name") String name, @JsonProperty("slots") Map<String, String> slots, @JsonProperty("confirmationStatus") String confirmationStatus) {
            this.name = name;
            this.slots = slots;
            this.confirmationStatus = confirmationStatus;
        }
    }

    @JsonProperty("currentIntent")
    CurrentIntent currentIntent;
    @JsonProperty("bot")
    Map<String, String> bot;
    @JsonProperty("userId")
    String userId;
    @JsonProperty("inputTranscript")
    String inputTranscript;
    @JsonProperty("invocationSource")
    String invocationSource;
    @JsonProperty("outputDialogMode")
    String outputDialogMode;
    @JsonProperty("messageVersion")
    String messageVersion;
    @JsonProperty("sessionAttributes")
    Map<String, String> sessionAttributes;

    @JsonCreator
    public RequestInput(@JsonProperty("currentIntent") CurrentIntent currentIntent, @JsonProperty("bot") Map<String, String> bot,
                        @JsonProperty("userId") String userId, @JsonProperty("inputTranscript") String inputTranscript,
                        @JsonProperty("invocationSource") String invocationSource, @JsonProperty("outputDialogMode") String outputDialogMode,
                        @JsonProperty("messageVersion") String messageVersion, @JsonProperty("sessionAttributes") Map<String, String> sessionAttributes) {
        this.currentIntent = currentIntent;
        this.bot = bot;
        this.userId = userId;
        this.inputTranscript = inputTranscript;
        this.invocationSource = invocationSource;
        this.outputDialogMode = outputDialogMode;
        this.messageVersion = messageVersion;
        this.sessionAttributes = sessionAttributes;
    }

    @Override
    public String toString() {
        return "Intent " + currentIntent.toString() + "; Bot " + bot.toString() + "; InputTranscript " + inputTranscript;
    }
}

In the handler class I just try to invoke RequestInput.toString() method, but I keep getting this error:

An error occurred during JSON parsing: java.lang.RuntimeException
java.lang.RuntimeException: An error occurred during JSON parsing
Caused by: java.io.UncheckedIOException: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.comelit.lex.LexIntercomCallValidate$RequestInput]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: lambdainternal.util.NativeMemoryAsInputStream@55d56113; line: 1, column: 2]
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.comelit.lex.LexIntercomCallValidate$RequestInput]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: lambdainternal.util.NativeMemoryAsInputStream@55d56113; line: 1, column: 2]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1106)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:296)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1511)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1102)

Upvotes: 2

Views: 1408

Answers (1)

user2248614
user2248614

Reputation: 63

Add a default constructor to class RequestInput. This error generally indicates the inability to instantiate the class to be mapped with the received JSON:

 public RequestInput() {}

Upvotes: 1

Related Questions