Java Webhook - 迁移到v2 - 无法将queryResult设置为java.util.LinkedHashMap [英] Java Webhook - Migration to v2 - Can not set queryResult to java.util.LinkedHashMap

查看:157
本文介绍了Java Webhook - 迁移到v2 - 无法将queryResult设置为java.util.LinkedHashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dialogflow v1
使用spring boot java作为webhook使用:
http://mvnrepository.org/artifact/ai.api/libai/1.6.12

i am using dialogflow v1 with spring boot java as webhook using: http://mvnrepository.org/artifact/ai.api/libai/1.6.12

现在我尝试升级到对话框流程v2使用此:
http://mvnrepository.org/artifact/com.google.apis/google-api-services-dialogflow/v2-rev2-1.23.0

now i try upgrading to dialogflow v2 using this: http://mvnrepository.org/artifact/com.google.apis/google-api-services-dialogflow/v2-rev2-1.23.0

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-dialogflow</artifactId>
    <version>v2-rev2-1.23.0</version>
</dependency>

目的:所以我不必自己解析/构建json
i有在mvnrepository搜索上找到它

purpose: so i dont have to take care of parsing/building the json myself i have found it over the mvnrepository search

我拦截了这个json:

i intercepted this json:

{
    "responseId": "72945ef4-0897-4705-a770-a12100162b45",
    "queryResult": {
        "queryText": "was gibts neues?",
        "action": "GetNewsFromWordpress",
        "parameters": {
            "allRequiredParamsPresent": true
        },
        "name": "projects/kreamont-abf6b/agent/intents/fe2c13a1-2e3f-48eb-a15a-660501c16807",
        "diagnosticInfo": {

        }
    },
    "languageCode": {
        "intentDetectionConfidence": 1.0
    },
    "displayName": {
        "payload": {

        }
    },
    "session": "projects/kreamont-abf6b/agent/sessions/e69aabe7-4f6a-4224-b881-8bbf31835ef2"
}           

杰克逊以某种方式无法绑定模型。我怎么能使用一些java lib所以我不必自己解析json?

jackson is somehow unable to bind the model. how can i use some java lib so i dont have to take care of parsing the json myself?

com.fasterxml.jackson.databind.JsonMappingException:无法设置com。 google.api.services.dialogflow.v2.model.GoogleCloudDialogflowV2QueryResult字段com.google.api.services.dialogflow.v2.model.GoogleCloudDialogflowV2WebhookRequest.queryResult to java.util.LinkedHashMap(通过参考链:com.google.api.services。 dialogflow.v2.model.GoogleCloudDialogflowV2WebhookRequest [queryResult])

com.fasterxml.jackson.databind.JsonMappingException: Can not set com.google.api.services.dialogflow.v2.model.GoogleCloudDialogflowV2QueryResult field com.google.api.services.dialogflow.v2.model.GoogleCloudDialogflowV2WebhookRequest.queryResult to java.util.LinkedHashMap (through reference chain: com.google.api.services.dialogflow.v2.model.GoogleCloudDialogflowV2WebhookRequest["queryResult"])

@RequestMapping(method = RequestMethod.POST, path = "fulfillment", consumes = MediaType.APPLICATION_JSON_VALUE)
public GoogleCloudDialogflowV2WebhookResponse getFulfillment(@RequestBody GoogleCloudDialogflowV2WebhookRequest request) {
    // HttpMessageNotReadableException 
    ...


推荐答案

该库取决于google-api-client。它有一个为传输对象定制的json解析器(如GoogleCloudDialogflowV2WebhookRequest)。这些对象从地图延伸。它们的元素标有Key注释。这就是普通的jackson或gson解析器失败的原因。您有多种选择:

The library is depending on the google-api-client. That has a customized json parser for the transfer objects (like GoogleCloudDialogflowV2WebhookRequest). These objects extending from a map. Their elements are marked with the Key annotation. That is why the normal jackson or gson parsers are failing. You have multiple options:


  1. 复制和修改转移对象(如果api将来会发生变化,则会很糟糕)

  2. 为使用google api json解析器的spring写一个httpmessageconverter(不知道会有多少努力)。

  3. 禁用端点的spring消息转换器并自行进行编组:

  1. copy and modify the transfer objects (bad if the api will change in the future)
  2. write a httpmessageconverter for spring that uses the google api json parser (do not know how much effort that would be)
  3. disable the spring message converters for your endpoint and do the marshalling on your own like this:

import com.google.api.client.json.JsonGenerator;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.dialogflow.v2.model.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;

@RestController
public class DialogflowWebhookController {

private static JacksonFactory jacksonFactory = JacksonFactory.getDefaultInstance();

@PostMapping("/dialogflow")
public Mono<String> webhook(@RequestBody String rawRequest) throws IOException {

    GoogleCloudDialogflowV2WebhookRequest request = jacksonFactory.createJsonParser(rawRequest)
        .parse(GoogleCloudDialogflowV2WebhookRequest.class);

    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = jacksonFactory.createJsonGenerator(stringWriter);
    GoogleCloudDialogflowV2WebhookResponse response = ...
    jsonGenerator.serialize(response);
    jsonGenerator.flush();
    return Mono.just(stringWriter.toString());
}
}


这篇关于Java Webhook - 迁移到v2 - 无法将queryResult设置为java.util.LinkedHashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆