For the complete documentation index, see llms.txt. This page is also available as Markdown.

Build a RESTful Web Service With Spring Boot and GridGain

Build a Spring Boot RESTful service backed by GridGain 8, using Spring Data repositories, DTOs, a service layer, and a REST controller over the World database.

Introduction

This tutorial walks you through the process of creating a RESTful web service that uses GridGain 8 as a high-performance, in-memory database. The service is a Spring Boot application with an embedded Apache Tomcat instance that interacts with a GridGain cluster via Spring Data repository abstraction.

Prerequisites

  • GridGain 8.10 binary distribution and a GridGain Enterprise or Ultimate license file (gridgain-license.xml)

  • Java Development Kit, version 17 or later

  • Your favorite IDE, such as IntelliJ IDEA, or Eclipse

What You Will Learn

  • How to start a GridGain cluster and load the World sample database

  • How to model GridGain data as Spring Data POJOs and DTOs

  • How to query GridGain through Spring Data repositories

  • How to expose a Spring REST controller backed by a GridGain thick client

Start the GridGain Cluster and Load the Sample Database

Before you develop the RESTful service, you start the GridGain cluster and load it with sample data. You can deploy GridGain in the environment of your choice. However, for this tutorial, you bootstrap the cluster in a bare-metal environment such as your personal Linux or Windows machine.

First, start a 2-node GridGain cluster:

  1. Download and unarchive the GridGain 8.10 binary package to some directory (this tutorial refers to it as {ignite} or $IGNITE_HOME).

  2. Copy your gridgain-license.xml file to the top level of that directory. The nodes read the license from there.

  3. Open a command-line tool (also known as "a terminal window") and go to the {ignite}/bin folder of the GridGain distribution.

  4. Using the default configuration settings, start the first cluster node.

  1. Open another command-line window and use the same command to start the second cluster node:

  1. After the two nodes form one cluster, look for the following message in the command line output to confirm the cluster is running:

Next, before you initialize a sample database in GridGain, tweak some parameters of the default SQL script that is shipped with the GridGain binary package:

  1. In a text editor, open the {ignite}/examples/sql/world.sql file.

  2. Replace the VALUE_TYPE=demo.model.Country parameter of the CREATE TABLE Country statement with the VALUE_TYPE=org.gridgain.demo.springdata.model.Country.

  3. Replace the KEY_TYPE=demo.model.CityKey parameter of the CREATE TABLE City statement with the KEY_TYPE=org.gridgain.demo.springdata.model.CityKey.

  4. Replace the VALUE_TYPE=demo.model.City parameter of the CREATE TABLE City statement with the VALUE_TYPE=org.gridgain.demo.springdata.model.City.

  5. Save the changes and close the editor.

You made the parameter changes to link the tables with data POJOs, which will be created later. With these settings, you can use the POJOs throughout the application logic, with Ignite taking care of serialization and deserialization to and from the raw data representation.

Finally, connect to the GridGain cluster (by using the SQLLine tool that is shipped with the GridGain binary distribution) and instantiate the database:

  1. Open another command-line window, and go to the {ignite}/bin folder.

  2. Use SQLLine to connect to the cluster:

  1. Enter random username and password.

  2. Load the sample database:

  1. After the database is created, quit SQLLine: !q

You can use the following script to perform these steps automatically:

Create the Spring Boot Application

Once the cluster is running, you can proceed with development of the RESTful application. Take advantage of the Spring Initializr tool that generates a Spring Boot project with predefined settings. Go to https://start.spring.io and generate a project with the following parameters:

Spring Project Initialization
  • Project: Maven project for Java

  • Spring Boot: 4.1.2 or a later 4.1 release. GridGain 8.10 uses Spring Framework 7 and Spring Data 4.1, which require Spring Boot 4.

  • Group: org.gridgain.demo

  • Artifact: ignite-spring-data-demo

  • Project name: ignite-spring-data-demo

  • Package name: org.gridgain.demo.springdata

  • Packaging: Jar

  • Java: 17 or later

  • Dependencies: Spring Web.

After you generate and download the project, open it with your favorite IDE and add additional dependencies to the pom.xml file:

  1. Add the GridGain repository, GridGain dependencies, and a JVM configuration for running the application:

    Keep the spring-boot-starter-parent and the other entries that Spring Initializr generated. The ignite-spring-data module brings in Spring Data Commons and Spring Transaction, so you do not add them separately. If your Initializr project lists spring-boot-starter-web, use the generated dependency as is.

Do a final configuration change, by requesting Spring to start an embedded Tomcat instance on port number 9000:

  1. Open the src/main/resources/application.properties file.

  2. Add server.port=9000 to the file.

Define Data Model POJOs

GridGain stores application records and entries in a cross-platform, language-neutral binary format. With this format, applications can query individual object fields. The applications can use SQL as well as use key-value APIs to read a full object entry that can be automatically deserialized to an associated POJO class instance.

In this tutorial, you define two types of POJOs. The first type, which represents the business objects of the RESTful service, is used internally throughout the application logic. The second type, which is called "data transfer objects (DTO)", is used by Spring Boot controllers to exchange the data via the external RESTful APIs.

Business Objects

In your project, create the org.gridgain.demo.springdata.model package for both business objects and DTO classes.

Add the org.gridgain.demo.springdata.model.Country class that is associated with the Country table (check the VALUE_TYPE parameter of the CREATE TABLE Country statement from the {ignite}/examples/sql/world.sql script):

Observations about the POJO class:

  • The names of the POJO fields and the names of the columns of the Country table need to correspond so that GridGain can transform the internal object representation to an instance of the POJO class.

  • The POJO includes all the fields of the Country table, except the primary key. The Code field is a primary key of the Country table, thus, it's not added to this POJO class. On the physical level, Ignite stores application records as key-value pairs, and your Country POJO should hold only the fields that belong to the value of a key-value pair. To retrieve a full key-value entry via a Spring Data repository, you can always use the Cache.Entry<KeyType,ValueType> notation, as shown later in the tutorial.

Define the business object classes by creating a POJO that is associated with the City table - org.gridgain.demo.springdata.model.City:

The City table defines a compound primary key (PRIMARY KEY (ID, CountryCode)) and you need a custom POJO class that encapsulates the fields of the key. Add the org.gridgain.demo.springdata.model.CityKey class:

Some observations about the POJO:

  • COUNTRYCODE field is annotated with @AffinityKeyMapped. This field is defined as the affinity key in the City table, and it must be annotated accordingly in the POJO class.

  • The names of the key fields are capitalized (COUNTRYCODE and ID). That is a workaround for a limitation that existed when this tutorial was written.

Data Transfer Objects

Create the org.gridgain.demo.springdata.model.CityDTO class that will be used by the Spring REST controllers.

The DTO encapsulates the fields of the internally used org.gridgain.demo.springdata.model.City and org.gridgain.demo.springdata.model.CityKey classes:

The DTO creates itself from a key and a value with the static of method, not with a constructor. Spring Boot 4 serializes JSON with Jackson 3, which would treat a public two-argument constructor as a way to create the object from JSON and fail to deserialize the request body of the PUT call later in this tutorial.

Add another DTO to hold all the fields of the Country table.

org.gridgain.demo.springdata.model.CountryDTO encapsulates the fields that are presented in the internally used org.gridgain.demo.springdata.model.Country POJO that you added previously, as well as the Code field, which is the table's primary key:

Create Spring Data Repositories

The Spring Data repositories will be located under the org.gridgain.demo.springdata.dao package.

Add the org.gridgain.demo.springdata.dao.CountryRepository repository class that will work with the data from the Country table:

  • This repository is associated with the Country table via the @RepositoryConfig (cacheName = "Country") annotation. The cache name is predefined in the {ignite}/examples/sql/world.sql script via the CACHE_NAME=Country parameter of the CREATE TABLE Country statement. Check this documentation page to discover why the cache and table terms co-exist in Ignite.

  • The repository implements a method that returns all countries that have a population equal to or greater than the requested value. The Cache.Entry<String,Country> object is used to retrieve a full key-value pair from Ignite. Here, String is the type of key (Code column, in the case of the Country table), and Country is the custom business objects that hold the fields of the value.

Create a repository that is named org.gridgain.demo.springdata.dao.CityRepository and that is associated with the City table:

In the repository, three methods demonstrate various usage patterns of the GridGain Spring Data integration:

  • The findAllByPopulationGreaterThanEqualOrderByPopulation method shows how to use Spring's query auto-generation feature to get a list of key-value pairs (Cache.Entry<CityKey, City>).

  • The findMostPopulatedCities method demonstrates how to pass in a direct query to GridGain to join the records of two tables. The method returns individual fields, rather than business model POJOs.

  • The findById method shows how to get key-value pairs if you need to filter the records by the value of a primary key field or fields.

Introduce Spring Service Abstraction

Instead of having the Spring REST controller work with the Spring repositories directly, you create a Spring service abstraction that takes care of the business-related logic and performs all necessary transformations between the internal business objects and the DTO classes.

In your project, create the org.gridgain.demo.springdata.service package and place the org.gridgain.demo.springdata.service.WorldDatabaseService class in it:

In addition to the methods that read data from the repositories upon controller request, the service implements the updateCityPopulation method, which shows how to update a record in Spring Data fashion. With the cityDao.findById(cityId) call, the method reads a record that needs to be updated. The method then adjusts the value of the population and writes the record back through the IgniteRepository.save(key, value) interface (see cityDao.save(entry.getKey(), entry.getValue()).

The implementation of updateCityPopulation method presents one of the ways for data modification with Spring Data repositories. Alternatively, you can always send UPDATE, DELETE or INSERT commands to GridGain by adding direct queries to the same repositories.

Develop the Spring REST Controller

Add the REST controller to your application, so the application will be capable of processing several HTTP-based API calls.

Create the org.gridgain.demo.springdata.controller package in your project and add the org.gridgain.demo.springdata.service.WorldDatabaseController class to it:

The controller intercepts a handful of GET and PUT requests that delegate the business logic execution to the Spring service abstraction.

Configure the GridGain Client

Next, you define the GridGain thick client as a Spring bean. The Ignite Spring Data repositories look the client up by the igniteInstance bean name. Place the class under the org.gridgain.demo.springdata package and name it org.gridgain.demo.springdata.DemoConfig:

With the default configuration, the client finds the two server nodes you started earlier through the default IP finder.

Finally, update the auto-generated org.gridgain.demo.springdata.IgniteSpringDataDemoApplication class by adding the @EnableIgniteRepositories annotation to enable GridGain Spring Data repositories.

Run the Application

You can build the project and start the application from your IDE or open a command-line window and execute the following Maven command from the project root folder:

Next, use the curl command-line tool to send HTTP requests, which will be intercepted and processed by your Spring controller.

First, get all the cities with a population equal to or larger than 8 million:

The response is as follows:

Second, get the top 10 most populated cities:

In response, the application joins the City and Country tables to produce the following result:

Finally, update the population value for the city with id 3507:

The data is updated, and the controller sends back the modified version of the specified city's record:

Congratulations! You've completed the tutorial and learned how to use GridGain with Spring Boot and Spring Data to build high-performance RESTful services.

This page is: Copyright © 2026 MariaDB. All rights reserved.

spinner

Last updated

Was this helpful?