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