How to create a SpringBoot Gemfire RestfulApi

Spring Data REST provides a mechanics for creating and retrieving an Object from Gemfire storage.
In the article, JavaSampleApproach will introduce you how to create a Gemfire RestfulAPI.

Related Articles:
How to start Embedded Gemfire Application with SpringBoot
Infinispan Cache Solution | Spring Cache | Spring Boot

I. Technologies for SpringBoot Gemfire Restful Api

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE

II. Overview

1. Project Structure

springboot gemfire restfulapi project structure

2. Step to do

– Create a Spring Boot project
– Create a Java Model class
– Config Gemfire storage
– Create a Gemfire repository
– Run & Check result

III. Practices

1. Create a Spring Boot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info, press Next then Finish, spring boot project will be created successfully.

Gemfire & Restful dependencies:

Open pom.xml file, add needed dependencies:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javasampleapproach.gemfirerestapi</groupId>
	<artifactId>gemfirerestapi</artifactId>
	<version>0.0.1</version>
	<packaging>jar</packaging>

	<name>GemfireRestAPI</name>
	<description>Gemfire Rest API</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-gemfire</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.shell</groupId>
            <artifactId>spring-shell</artifactId>
            <version>1.0.0.RELEASE</version>
            <scope>runtime</scope>
        </dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
         <repository>
             <id>spring-releases</id>
             <url>http://repo.spring.io/libs-release</url>
         </repository>
     </repositories>

</project>

2. Create a Java Model class

– Create a simple Java model:


package com.javasampleapproach.gemfirerestapi.model;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.gemfire.mapping.Region;
 
@Region("customer")
public class Customer {
	
	private static AtomicLong COUNTER = new AtomicLong(0L);
	
    @Id
    private Long id;
    private String firstname;
    private String lastname;
    private int age;
     
    @PersistenceConstructor
	public Customer() {
		this.id = COUNTER.incrementAndGet();
	}
 
    public String getFirstname() {
        return firstname;
    }
 
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
 
     
    public String getLastname() {
        return lastname;
    }
 
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
     
    public String toString(){
        return "firstname: " + firstname + " ,lastname: " + lastname + " ,age: " + age; 
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
}

3. Config Gemfire storage

Config CacheFactoryBean and LocalRegionFactoryBean


package com.javasampleapproach.gemfirerestapi.config;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.LocalRegionFactoryBean;

import com.gemstone.gemfire.cache.GemFireCache;
import com.javasampleapproach.gemfirerestapi.model.Customer;

@Configuration
public class GemfireConfig {
    @Bean
    Properties gemfireProperties() {
        Properties gemfireProperties = new Properties();
        gemfireProperties.setProperty("name", "EmbeddedGemfireApplication");
        gemfireProperties.setProperty("mcast-port", "0");
        return gemfireProperties;
    }
     
    @Bean
    CacheFactoryBean gemfireCache() {
        CacheFactoryBean gemfireCache = new CacheFactoryBean();
        gemfireCache.setProperties(gemfireProperties());
        return gemfireCache;
    }
     
    @Bean
    LocalRegionFactoryBean customerRegion(final GemFireCache cache) {
        LocalRegionFactoryBean customerRegion = new LocalRegionFactoryBean<>();
        customerRegion.setCache(cache);
        customerRegion.setName("customer");
        customerRegion.setPersistent(false);
        return customerRegion;
    }
}

4. Create a Gemfire repository

Create a Gemfire repository interface by extends CrudRepository Spring interface.
@RepositoryRestResource is optional, it is used to redefine a default path.


package com.javasampleapproach.gemfirerestapi.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.javasampleapproach.gemfirerestapi.model.Customer;

@RepositoryRestResource(collectionResourceRel = "customer", path = "customer")
public interface CustomerRepository extends CrudRepository {
	Customer findByFirstname(@Param("firstname") String firstname);
    Customer findByLastname(@Param("lastname") String lastname);
    Iterable findByAgeGreaterThan(@Param("age") int age);
    Iterable findByAgeLessThan(@Param("age") int age);
}

In main class, enable GemfireRepositories by
@EnableGemfireRepositories


package com.javasampleapproach.gemfirerestapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;

@EnableGemfireRepositories
@SpringBootApplication
public class SpringBootGemfireRestfulApiApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootGemfireRestfulApiApplication.class, args);
	}
}

5. Run & Check result

Run project with Spring Boot Application mode.
Test time:

– Store 2 customers to Gemfire Caching:
springboot gemfire restfulapi - post entity

springboot gemfire restfulapi

– Retrieve all customers from Gemfire:
springboot gemfire restfulapi - get all

– Search RestfulAPI:
springboot  gemfire restfulapi search api

– Retrieve a customer from Gemfire:
 springboot gemfire restfulapi - get customer by id

– Find a customer by firstname:
springboot gemfire restfulapi - findbyfirstname

– Find a customer by lastname:
springboot gemfire restfulapi - findbylastname

– Find customers by age:
springboot gemfire restfulapi  - findbyagegreaterthan

springboot  gemfire restfulapi - findbyagelessthan

IV. Sourcecode

GemfireRestAPI

4 thoughts on “How to create a SpringBoot Gemfire RestfulApi”

  1. Good post.

    I have few queries here:
    1. How to view the saved data on UI like mysql, oracle, etc… ?
    2. How to deal with different tables/entities if they are having mappings ?
    3. How we can use this in Micro services architecture ?

    Thanks.

  2. I have viewed that wise real estate agents all over the place are starting to warm up to FSBO Advertising and marketing. They are realizing that it’s more than merely placing a sign post in the front property. It’s really regarding building human relationships with these traders who sooner or later will become purchasers. So, whenever you give your time and effort to serving these suppliers go it alone — the “Law connected with Reciprocity” kicks in. Good blog post.

Leave a Reply

Your email address will not be published. Required fields are marked *