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:



	4.0.0

	com.javasampleapproach.gemfirerestapi
	gemfirerestapi
	0.0.1
	jar

	GemfireRestAPI
	Gemfire Rest API

	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.3.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		
		
		
            org.springframework.boot
            spring-boot-starter-data-gemfire
        
        
            org.springframework.boot
            spring-boot-starter-data-rest
        
        
            org.springframework.shell
            spring-shell
            1.0.0.RELEASE
            runtime
        

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
         
             spring-releases
             http://repo.spring.io/libs-release
         
     



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



By grokonez | December 31, 2016.

Last updated on June 4, 2017.



Related Posts


1 thought 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.

Got Something To Say:

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

*