适用于Amazon Lex AWS Lambda钩子的Jackson JSON反序列化

时间:2017-06-26 09:25:07

标签: java json amazon-web-services aws-lambda aws-sdk

我在AWS Lex Lambda钩子中实现了反序列化的问题。我有一个AWS Lambda函数来验证用户输入,但我不断收到JSONMapping错误。 Lex json是这样的: {   " currentIntent":{     " name":" intent-name",     " slot":{       " slot-name":" value",       " slot-name":" value",       " slot-name":" value"     },     " confirmationStatus":"无,确认或拒绝(意图确认,如果已配置)",   },   " bot":{     " name":" bot-name",     "别名":" bot-alias",     "版本":" bot-version"   },   " userId":"在对Amazon Lex的POST请求中指定的用户ID。",   " inputTranscript":"用于处理请求的文本",   " invocationSource":" FulfillmentCodeHook或DialogCodeHook",   " outputDialogMode":"文本或语音,基于运行时API请求中的ContentType请求标头",   " messageVersion":" 1.0",   " sessionAttributes":{      " key1":" value1",      " key2":" value2"   } } 我用于反序列化此JSON的Java bean是: 公共类RequestInput {     公共类CurrentIntent {         @JsonProperty("名称&#34)         字符串名称;         @JsonProperty("槽&#34)         Map< String,String>插槽;         @JsonProperty(" confirmationStatus&#34)         String confirmationStatus;         public CurrentIntent(@JsonProperty(" name")String name,@ JsonProperty(" slots")Map< String,String> slots,@ JsonProperty(" confirmationStatus")字符串confirmStatus){             this.name = name;             this.slots = slots;             this.confirmationStatus = confirmationStatus;         }     }     @JsonProperty(" currentIntent&#34)     CurrentIntent currentIntent;     @JsonProperty("机器人&#34)     Map< String,String> BOT;     @JsonProperty("用户id&#34)     String userId;     @JsonProperty(" inputTranscript&#34)     String inputTranscript;     @JsonProperty(" invocationSource&#34)     String invocationSource;     @JsonProperty(" outputDialogMode&#34)     String outputDialogMode;     @JsonProperty(" messageVersion&#34)     String messageVersion;     @JsonProperty(" sessionAttributes&#34)     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;     }     @覆盖     public String toString(){         返回"意图" + currentIntent.toString()+&#34 ;; Bot" + bot.toString()+&#34 ;; InputTranscript" + inputTranscript;     } } 在处理程序类中,我只是尝试调用RequestInput.toString()方法,但我不断收到此错误: JSON解析期间发生错误:java.lang.RuntimeException java.lang.RuntimeException:JSON解析期间发生错误 引起:java.io.UncheckedIOException:com.fasterxml.jackson.databind.JsonMappingException:找不到类型[simple type,class com.comelit.lex.LexIntercomCallValidate $ RequestInput]的合适构造函数:无法从JSON对象实例化(缺少默认值)构造函数或创建者,或者可能需要添加/启用类型信息?) 在[来源:lambdainternal.util.NativeMemoryAsInputStream@55d56113; line:1,column:2] 引起:com.fasterxml.jackson.databind.JsonMappingException:找不到类型[simple type,class com.comelit.lex.LexIntercomCallValidate $ RequestInput]的合适构造函数:无法从JSON对象实例化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息吗?) 在[来源: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) 在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)

1 个答案:

答案 0 :(得分:1)

为类RequestInput添加默认构造函数。此错误通常表示无法实例化要使用收到的JSON映射的类:

 public RequestInput() {}