Initialize project using Spring Boot Initializr https://start.spring.io/
Add production dependencies:
pom.xml
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>spring-boot-starter-data-aerospike</artifactId>
</dependency>
Add test dependencies:
pom.xml
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-aerospike</artifactId>
<scope>test</scope>
</dependency>
pom.xml
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>
Create Customer
entity:
Customer.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
@Id
private String id;
private String firstName;
@Indexed(type = IndexType.STRING, collectionType = IndexCollectionType.DEFAULT)
private String lastName;
private long age;
}
Create repository interface:
SyncCustomerRepository.java
public interface SyncCustomerRepository extends AerospikeRepository<Customer, String>, CrudRepository<Customer, String> {
List<Customer> findByLastNameOrderByFirstNameAsc(String lastName);
}
Create controller:
SyncCustomerController.java
@Slf4j
@RequestMapping("/sync")
@RestController
public class SyncCustomerController {
@Autowired
private SyncCustomerRepository repository;
@PostMapping("/customer")
public Customer createCustomer(@RequestBody Customer customer) {
Customer saved = repository.save(customer);
log.info("Created {}", saved);
return saved;
}
@GetMapping("/customer/{id}")
public ResponseEntity<Customer> getCustomerById(@PathVariable(value = "id") String customerId) {
return repository.findById(customerId)
.map(body -> {
log.info("Retrieved " + body);
return ResponseEntity.ok(body);
})
.orElse(ResponseEntity.noContent().build());
}
@PutMapping("/customer/{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable(value = "id") String customerId,
@Valid @RequestBody Customer customer) {
return repository.findById(customerId)
.map(existing -> {
existing.setFirstName(customer.getFirstName());
existing.setLastName(customer.getLastName());
existing.setAge(customer.getAge());
Customer result = repository.save(existing);
log.info("Updated " + result);
return result;
})
.map(ResponseEntity::ok)
.orElse(ResponseEntity.noContent().build());
}
@DeleteMapping("/customer/{id}")
public ResponseEntity<Void> deleteCustomer(@PathVariable(value = "id") String customerId) {
try {
repository.deleteById(customerId);
return ResponseEntity.ok().build();
} catch (DataRetrievalFailureException exception) {
return ResponseEntity.noContent().build();
}
}
@DeleteMapping("/customers")
public void deleteCustomers() {
repository.deleteAll();
log.info("Deleted all customers");
}
@GetMapping("/customers")
public Iterable<Customer> getAllCustomers() {
Iterable<Customer> all = repository.findAll();
log.info("Retrieved all customers");
return all;
}
@GetMapping("/customers/search")
public List<Customer> getAllCustomersByLastName(@RequestParam(value = "lastName") String lastName) {
List<Customer> result = repository.findByLastNameOrderByFirstNameAsc(lastName);
log.info("Retrieved all customers with last name " + lastName);
return result;
}
}
Minimal application is up and running.
Voila.