×
☰ See All Chapters

Aerospike Java Example using Spring Boot

Create java Project using Maven

In the command prompt execute the following maven command to generate Maven supported Java project named as “AerospikeExampleUsingMavenAndSpringboot”.

mvn archetype:generate -DgroupId=com.java4coding -DartifactId= AerospikeExampleUsingMavenAndSpringboot -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

aerospike-java-example-using-spring-boot-0
 

This command creates a new maven Java project with the name “AerospikeExampleUsingMavenAndSpringboot”, with complete directory structure.

aerospike-java-example-using-spring-boot-1
 
aerospike-java-example-using-spring-boot-2
 

Import project into Intellij IDE

In Intellij IDE, Choose File –> New –> Module from Existing Sources –>Select pom.xml from newly created maven project, Click OK.

aerospike-java-example-using-spring-boot-3
 
aerospike-java-example-using-spring-boot-4
 

Add dependencies in pom.xml

Add the maven parent, add the below aerospike, sprinboot and other dependencies in pom.xml file.

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
   
<modelVersion>4.0.0</modelVersion>
   
<groupId>com.java4coding</groupId>
   
<artifactId>AerospikeExampleUsingMavenAndSpringboot</artifactId>
   
<packaging>jar</packaging>
   
<version>1.0-SNAPSHOT</version>
   
<name>AerospikeExampleUsingMavenAndSpringboot</name>
   
<parent>
       
<groupId>org.springframework.boot</groupId>
       
<artifactId>spring-boot-starter-parent</artifactId>
       
<version>2.7.3</version>
   
</parent>
   
<dependencies>
       
<dependency>
           
<groupId>junit</groupId>
           
<artifactId>junit</artifactId>
           
<version>3.8.1</version>
           
<scope>test</scope>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework.boot</groupId>
           
<artifactId>spring-boot-starter-web</artifactId>
       
</dependency>
       
<dependency>
           
<groupId>org.apache.tomcat.embed</groupId>
           
<artifactId>tomcat-embed-jasper</artifactId>
           
<scope>provided</scope>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework.boot</groupId>
           
<artifactId>spring-boot-devtools</artifactId>
           
<optional>true</optional>
       
</dependency>
       
<dependency>
           
<groupId>com.aerospike</groupId>
           
<artifactId>spring-data-aerospike</artifactId>
           
<version>2.4.0.RELEASE</version>
       
</dependency>
       
<dependency>
           
<groupId>org.projectlombok</groupId>
           
<artifactId>lombok</artifactId>
           
<optional>true</optional>
       
</dependency>
   
</dependencies>
   
<properties>
       
<java.version>1.8</java.version>
   
</properties>
</project>

 

Configure aerospike connection details in application.properties

Right click on main folder and select New>Directory

aerospike-java-example-using-spring-boot-5

Now you select resources and press Enter

aerospike-java-example-using-spring-boot-6

Now you create application.properties file inside resources directory and add below property values.

application.properties

# aerospike
aerospike.host=localhost
aerospike.port=3000
aerospike.namespace=demo

 

Create aerospike repository, service, controller and spring boot code

AerospikeUserRepository.java

package com.java4coding.repositories;

import com.java4coding.dto.Student;
import org.springframework.data.aerospike.repository.AerospikeRepository;

public interface AerospikeUserRepository extends AerospikeRepository<Student, Integer> {
}

 

 

Student.java

package com.java4coding.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.aerospike.mapping.Document;
import org.springframework.data.annotation.Id;

@Data
@Document
@AllArgsConstructor
public class Student {
   
@Id
   
private int id;
   
private String firstName;
   
private String lastName;
   
private int age;
   
private String gender;
}

 

 

StudentService.java

package com.java4coding.services;

import com.java4coding.dto.Student;
import com.java4coding.repositories.AerospikeUserRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@AllArgsConstructor
public class StudentService {
   AerospikeUserRepository
aerospikeUserRepository;

   
public Optional<Student> readUserById(int id) {
       
return aerospikeUserRepository.findById(id);
   }

   
public void addUser(Student user) {
       
aerospikeUserRepository.save(user);
   }

   
public void removeUserById(int id) {
       
aerospikeUserRepository.deleteById(id);
   }
}

 

 

AerospikeConfigurationProperties.java

package com.java4coding.configuration;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties
(prefix = "aerospike")
public class AerospikeConfigurationProperties {
   
private String host;
   
private int port;
   
private String namespace;
}

 

 

AerospikeConfiguration.java

package com.java4coding.configuration;

import com.aerospike.client.Host;
import com.java4coding.repositories.AerospikeUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.aerospike.config.AbstractAerospikeDataConfiguration;
import org.springframework.data.aerospike.repository.config.EnableAerospikeRepositories;

import java.util.Collection;
import java.util.Collections;

@Configuration
@EnableConfigurationProperties
(AerospikeConfigurationProperties.class)
@EnableAerospikeRepositories(basePackageClasses = AerospikeUserRepository.class)
public class AerospikeConfiguration extends AbstractAerospikeDataConfiguration {
   
@Autowired
   
private AerospikeConfigurationProperties aerospikeConfigurationProperties;

   
@Override
   
protected Collection<Host> getHosts() {
       
return Collections.singleton(new Host(aerospikeConfigurationProperties.getHost(),
               
aerospikeConfigurationProperties.getPort()));
   }

   
@Override
   
protected String nameSpace() {
       
return aerospikeConfigurationProperties.getNamespace();
   }
}

 

 

StudentController.java

package com.java4coding.controller;

import com.java4coding.dto.Student;
import com.java4coding.services.StudentService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@AllArgsConstructor
public class StudentController {
   StudentService
userService;

   
@GetMapping("/readStudent/{id}")
   
public Optional<Student> readUserById(@PathVariable("id") Integer id) {
       
return userService.readUserById(id);
   }

   
@PostMapping("/addStudent")
   
public String addUser(@RequestBody Student user) {
       
userService.addUser(user);
       
return "Success";
   }

   
@DeleteMapping("/deleteStudent/{id}")
   
public String deleteUserById(@PathVariable("id") Integer id) {
       
userService.removeUserById(id);
       
return "Success";
   }
}

 

 

AerospikeDemo.java

package com.java4coding;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan
("com.java4coding")
public class AerospikeDemo {
   
public static void main(String[] args) {
       SpringApplication.
run(AerospikeDemo.class, args);
   }
}

 

Intellij Project Structure

aerospike-java-example-using-spring-boot-7
 

Run the application

Make sure aerospike is running.

Make sure the schema is created.

Right click on AerospikeDemo.java and select Run ‘AerospikeDemo.main()’

aerospike-java-example-using-spring-boot-8
 

Spring boot will start and now you can invoke the controller.

aerospike-java-example-using-spring-boot-9
 

Save the below JSON to a file and import it to POSTMAN. If you don’t have Postman installed on your local computer you can download it here: https://www.postman.com/downloads/

AerospikeTest.postman_collection.json

{
 
"info": {
     
"_postman_id": "dd39dca3-8f15-444a-bc54-a555c4dd3af3",
     
"name": "AerospikeTest",
     
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
 
},
 
"item": [
     {
       
"name": "https://localhost:8080/users",
       
"request": {
           
"method": "POST",
           
"header": [
              {
                 
"key": "Content-Type",
                 
"value": "application/json",
                 
"type": "default"
             
}
           ],
           
"body": {
             
"mode": "raw",
             
"raw": "{\r\n    \"id\": 1,\r\n    \"firstName\": \"Manu\",\r\n    \"lastName\": \"Manjunatha\",\r\n    \"age\": 20,\r\n    \"gender\":\"Male\"\r\n}"
           
},
           
"url": {
             
"raw": "https://localhost:8080/addStudent",
             
"protocol": "http",
             
"host": [
                 
"localhost"
             
],
             
"port": "8080",
             
"path": [
                 
"addStudent"
             
]
           }
        },
       
"response": []
     },
     {
       
"name": "https://localhost:8080/users",
       
"request": {
           
"method": "GET",
           
"header": [],
           
"url": {
             
"raw": "https://localhost:8080/readStudent/1",
             
"protocol": "http",
             
"host": [
                 
"localhost"
             
],
             
"port": "8080",
             
"path": [
                 
"readStudent",
                 
"1"
             
]
           }
        },
       
"response": []
     },
     {
       
"name": "https://localhost:8080/users",
       
"request": {
           
"method": "DELETE",
           
"header": [],
           "url": {
             
"raw": "https://localhost:8080/deleteStudent/1",
             
"protocol": "http",
             
"host": [
                 
"localhost"
             
],
             
"port": "8080",
             
"path": [
                 
"deleteStudent",
                 
"1"
             
]
           }
        },
       
"response": []
     }
  ]
}

 

aerospike-java-example-using-spring-boot-10
 

Add Student

Open POST request and click on Send.

aerospike-java-example-using-spring-boot-11
 

Read Student

Open GET request and click on Send.

aerospike-java-example-using-spring-boot-12
 

Delete Student

Open DELETE request and click on Send.

aerospike-java-example-using-spring-boot-13
 

 

 

 

 

 

 

 


All Chapters
Author