Dialoglfow Webhook API with Spring Boot

Carvia Tech | July 26, 2020 | 2 min read | 531 views

In this tutorial we will learn how to implement Google Dialogflow response fulfilment webhook API with a Spring Boot application.

Gradle setup

Create a simple Spring Boot starter project using Spring Initializer and add the Google API services dependency for dialoglow.

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.3.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'

dependencies {
    implementation group: 'com.google.apis', name: 'google-api-services-dialogflow', version: 'v2-rev124-1.25.0'

You can check latest version of Google Dialogflow api from Maven Repository

Deserializer configuration

Default Jackson deserializer cannot deserialize webhook response from Google Dialoglow API. For consuming Dialogflow webhook response, we need to use instances of JacksonFactory or GsonFactory provided by google-api-services-dialogflow.

DialogFlowConfig - JacksonFactory & GsonFactory
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class DialogFlowConfiguration {

    public JacksonFactory jacksonFactory() {
        return JacksonFactory.getDefaultInstance();

If you do not want to use JacksonFactory, then use Gsonfactory which should work equally well.

Creating GsonFactory
public GsonFactory gsonFactory() {
    return GsonFactory.getDefaultInstance();

Creating a Controller

Next is to create a Controller that will accept incoming requests from Google Dialogflow WebHook API

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

public class DialogFlowWebhookController {

    private final JacksonFactory jacksonFactory;

    @PostMapping(value = "/dialogflow-webhook", produces = {MediaType.APPLICATION_JSON_VALUE})
    public String webhook(@RequestBody String rawData) {
        //Step 1. Parse the request
        GoogleCloudDialogflowV2WebhookRequest request = jacksonFactory

        //Step 2. Process the request
        //Step 3. Build the response message
        GoogleCloudDialogflowV2IntentMessage msg = new GoogleCloudDialogflowV2IntentMessage();
        GoogleCloudDialogflowV2IntentMessageText text = new GoogleCloudDialogflowV2IntentMessageText();
        text.setText("Welcome to Spring Boot");

        GoogleCloudDialogflowV2WebhookResponse response = new GoogleCloudDialogflowV2WebhookResponse();
        StringWriter stringWriter = new StringWriter();
        JsonGenerator jsonGenerator = jacksonFactory.createJsonGenerator(stringWriter);
        return stringWriter.toString();

Will add more on this later on.

Top articles in this category:
  1. Slack Webhook Message from Spring Boot
  2. Basic Auth Security in Spring Boot 2
  3. Spring Boot 2.0 Reactive Web Performance Metrics
  4. Sendgrid Dynamic Templates with Spring Boot
  5. Testing web layer in Spring Boot using WebMvcTest
  6. Redis rate limiter in Spring Boot
  7. SendGrid Attachments with Spring Boot

Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info