gpt4 book ai didi

java - 使用 Actions On Google 从 Webhook 请求获取连接池

转载 作者:行者123 更新时间:2023-12-02 03:28:04 25 4
gpt4 key购买 nike

我尝试从触发的 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
}
...
}

最佳答案

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

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

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

关于java - 使用 Actions On Google 从 Webhook 请求获取连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56899663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com