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

时间:2019-07-05 08:48:05

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

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

1 个答案:

答案 0 :(得分:0)

我看不到ActionRequest提供了ServletContext,因为它与处理请求的方式无关。

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

创建一个传递的构造函数,这也是一个选择。

相关问题