【问题标题】:Getting connection pool from webhook request with Actions On Google使用 Actions On Google 从 webhook 请求中获取连接池
【发布时间】:2019-07-05 08:48:05
【问题描述】:

我正在尝试从触发的 webhook 意图使用与我的数据库相同的连接池。我正在使用基本样板文件 (https://github.com/actions-on-google/dialogflow-webhook-boilerplate-java)。

每个触发的意图都会向我返回ActionRequest,但是,它不包含ServletContext。那么,如何使用actions-on-google-java 库获得正常的请求上下文? (https://github.com/actions-on-google/actions-on-google-java/blob/master/src/main/kotlin/com/google/actions/api/ActionRequest.kt)


我已尝试寻找其他方法来获取池,但我对 Java 的经验有限。

// this is what i would like to achieve but ActionRequest is limited
@ForIntent("DB test")
public ActionResponse DbTest(ActionRequest request)
{
    ...
    DataSource pool = (DataSource) request.getServletContext().getAttribute("my-pool");
    try (Connection conn = pool.getConnection()) 
    { 
        // do database stuff
    }
    ...
}

【问题讨论】:

标签: java dialogflow-es actions-on-google google-home dialogflow-es-fulfillment


【解决方案1】:

我没有看到 ActionRequest 提供 ServletContext,因为它独立于处理请求的内容。

不太优雅,但您可以在调用 handleRequest 之前在您的 MyActionsApp 中创建一个方法(因为您使用的是基本样板文件)以根据您的请求 (HttpServletRequest) 设置 ServletContext。

创建一个构造函数来传递它也是一个选项。

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多