Dialoglfow fulfillment with Spring Boot

Upasana | November 21, 2020 | 2 min read | 649 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. SendGrid Attachments with Spring Boot
  2. Sendgrid Dynamic Templates with Spring Boot
  3. Feign Client Logging and connection timeout
  4. Elasticsearch with Spring Boot + Spring Data
  5. SendGrid emails in Spring Boot
  6. Spring Boot multipart file upload server
  7. Basic Auth Security in Spring Boot 2

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.