编程语言
首页 > 编程语言> > Java Webhook – 迁移到v2 – 无法将queryResult设置为java.util.LinkedHashMap

Java Webhook – 迁移到v2 – 无法将queryResult设置为java.util.LinkedHashMap

作者:互联网

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

现在我尝试使用以下方法升级到dialogflow v2:
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
我在mvnrepository搜索中找到了它

我拦截了这个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?

com.fasterxml.jackson.databind.JsonMappingException:无法将com.google.api.services.dialogflow.v2.model.GoogleCloudDialogflowV2QueryResult字段com.google.api.services.dialogflow.v2.model.GoogleCloudDialogflowV2WebhookRequest.queryResult设置为java.util .LinkedHashMap(通过参考链: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解析器失败的原因.您有多种选择:

>复制和修改传输对象(如果api将来会改变,则会很糟糕)
>为使用google api json解析器的spring写一个httpmessageconverter(不知道会有多少努力)
>为您的终端禁用弹出消息转换器并自行进行编组,如下所示:

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,dialogflow
来源: https://codeday.me/bug/20190705/1388951.html