【问题标题】:Jackson JSON deserialization for Amazon Lex AWS Lambda hook用于 Amazon Lex AWS Lambda 挂钩的 Jackson JSON 反序列化
【发布时间】:2017-11-29 02:35:40
【问题描述】:

我在 AWS Lex Lambda 挂钩中实现的反序列化存在问题。我有一个 AWS Lambda 函数来验证用户输入,但我不断收到 JSONMapping 错误。 Lex json 是这样的:

{
  "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 是:

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: 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)

【问题讨论】:

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


    【解决方案1】:

    为 RequestInput 类添加一个默认构造函数。此错误一般表示无法实例化要与接收到的 JSON 进行映射的类:

     public RequestInput() {}
    

    【讨论】:

    • @Gili 你们找到解决这个问题的方法了吗?这里有同样的问题,当请求输入被反序列化时,@JsonCreator 完全被忽略了。
    • @FrançoisBeaune 我猜 AWS 使用的是不支持 @JsonCreator 的旧版 Jackson。我通过向 lambda 发送 String 输入来解决该问题,然后使用最新版本的 Jackson 实例化我自己的 ObjectMapper 并自己反序列化对象。这非常有效。
    • 谢谢@Gili,这很有道理,我们会试试的!
    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多