我正在尝试使用 Spring Boot 和 Dialogflow 在 google 上创建操作。我在其中尝试使用可用的java库https://github.com/actions-on-google/actions-on-google-java
但无法理解我应该如何在 Spring Boot 应用程序中实现这些注释。例如:@ForIntent
我已尝试使用 App Engine 入口点的样板代码 https://github.com/actions-on-google/dialogflow-webhook-boilerplate-java 我能够运行此代码,但无法理解其在 Spring boot 应用程序中的实现。
在 Spring 启动中: 我们在应用程序中使用@RestController来映射请求
但是对于 google 上的操作,只会有一个请求链接,我们可以作为 Fulfillment webhook 提供。那么我应该在代码中的哪里使用 @ForIntent 来识别 Intent 并更改请求正文和响应正文。
最佳答案
我实际上曾经做过这一点,并基于 Silly Name Maker样本。我基于 Spring Boot 规范示例之一,因此我不会保证这是“最佳”,但它是一个非常干净的实现。
您可以保持 SillyNameMakerApp
不变,不进行修改。除了 ActionsServlet,您还可以创建一个 Spring Boot 包装器,如下所示:
@SpringBootApplication
public class HelloworldApplication {
private static final Logger LOG = LoggerFactory.getLogger(SillyNameMakerApp.class);
private final App actionsApp = new SillyNameMakerApp();
@Value("${TARGET:World}")
String message;
@RestController
class HelloworldController {
@GetMapping("/")
String serveAck() {
return "App is listening but requires valid POST request to respond with Action response.";
}
@RequestMapping(value = "/", method = RequestMethod.POST, produces = { "application/json" })
String serveAction(@RequestBody String body, @RequestHeader Map<String, String> headers) {
try {
return actionsApp.handleRequest(body, headers).get();
} catch (InterruptedException | ExecutionException e) {
return handleError(e);
}
}
private String handleError(Exception e) {
e.printStackTrace();
LOG.error("Error in App.handleRequest ", e);
return "Error handling the intent - " + e.getMessage();
}
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
关于java - 如何使用 Spring Boot 和 Java 库在 google 上创建操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55326082/