2017-06-26 12 views
2

AWS Lexラムダフックに実装されているデシリアライゼーションに問題があります。ユーザー入力を検証するAWSラムダ関数がありますが、JSONMappingエラーが発生しています。Amazon JSONの逆シリアル化Amazon Lex AWSラムダフック

{ 
    "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" 
    } 
} 

そして、このJSONをデシリアライズのための私のJava Beanは次のとおりです: レックスのJSONは、このようなものですハンドラクラスで

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; 
    } 
} 

私はちょうどRequestInput.toString()メソッドを呼び出すようにしようと、しかし、私はこのエラーが発生し続ける:

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: [email protected]; 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: [email protected]; 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) 

答えて

1

RequestInputクラスにデフォルトのコンストラクタを追加します。このエラーは、一般に、受信したJSONにマップされるクラスをインスタンス化できないことを示します。

public RequestInput() {} 
+1

確かに '@ JsonCreator'は機能しません。 – Gili

+0

@Gili皆さん、この問題の解決策を見つけましたか?ここで同じ問題が発生した場合、リクエスト入力が逆シリアル化されたときに '@ JsonCreator'は完全に無視されます。 –

+1

@FrançoisBeauneAWSは '@ JsonCreator'をサポートしない古いバージョンのジャクソンを使用していると思います。私はラムダに 'String'入力を送り、最後にジャクソンの最新バージョンを使って自分自身の' ObjectMapper'をインスタンス化し、オブジェクトを自分自身で逆シリアル化することで問題を解決しました。これは完全に機能します。 – Gili

関連する問題