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
Apache Maven 3.6.3 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:
Download and unarchive the GridGain 8.10 binary package to some directory (this tutorial refers to it as
{ignite}or$IGNITE_HOME).Copy your
gridgain-license.xmlfile to the top level of that directory. The nodes read the license from there.Open a command-line tool (also known as "a terminal window") and go to the
{ignite}/binfolder of the GridGain distribution.Using the default configuration settings, start the first cluster node.
Open another command-line window and use the same command to start the second cluster node:
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:
In a text editor, open the
{ignite}/examples/sql/world.sqlfile.Replace the
VALUE_TYPE=demo.model.Countryparameter of theCREATE TABLE Countrystatement with theVALUE_TYPE=org.gridgain.demo.springdata.model.Country.Replace the
KEY_TYPE=demo.model.CityKeyparameter of theCREATE TABLE Citystatement with theKEY_TYPE=org.gridgain.demo.springdata.model.CityKey.Replace the
VALUE_TYPE=demo.model.Cityparameter of theCREATE TABLE Citystatement with theVALUE_TYPE=org.gridgain.demo.springdata.model.City.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:
Open another command-line window, and go to the
{ignite}/binfolder.Use SQLLine to connect to the cluster:
Enter random username and password.
Load the sample database:
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:

Project: Maven project for Java
Spring Boot:
4.1.2or 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.demoArtifact:
ignite-spring-data-demoProject name:
ignite-spring-data-demoPackage name:
org.gridgain.demo.springdataPackaging: 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:
Add the GridGain repository, GridGain dependencies, and a JVM configuration for running the application:
Keep the
spring-boot-starter-parentand the other entries that Spring Initializr generated. Theignite-spring-datamodule brings in Spring Data Commons and Spring Transaction, so you do not add them separately. If your Initializr project listsspring-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:
Open the
src/main/resources/application.propertiesfile.Add
server.port=9000to 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
Countrytable 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
Countrytable, except the primary key. TheCodefield is a primary key of theCountrytable, thus, it's not added to this POJO class. On the physical level, Ignite stores application records as key-value pairs, and yourCountryPOJO 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 theCache.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:
COUNTRYCODEfield is annotated with@AffinityKeyMapped. This field is defined as the affinity key in theCitytable, and it must be annotated accordingly in the POJO class.The names of the key fields are capitalized (
COUNTRYCODEandID). 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
Countrytable via the@RepositoryConfig (cacheName = "Country")annotation. The cache name is predefined in the{ignite}/examples/sql/world.sqlscript via theCACHE_NAME=Countryparameter of theCREATE TABLE Countrystatement. Check this documentation page to discover why thecacheandtableterms 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,Stringis the type of key (Codecolumn, in the case of theCountrytable), andCountryis 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
findAllByPopulationGreaterThanEqualOrderByPopulationmethod shows how to use Spring's query auto-generation feature to get a list of key-value pairs (Cache.Entry<CityKey, City>).The
findMostPopulatedCitiesmethod 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
findByIdmethod 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()).
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.
Related
Spring Boot reference documentation for details on the Spring Boot features used here
This page is: Copyright © 2026 MariaDB. All rights reserved.
Last updated
Was this helpful?

