Integrating AWS DynamoDB with Spring Boot - Demonstrating basic CRUD operations.
Here’s a complete example of integrating AWS DynamoDB with Spring Boot, demonstrating basic CRUD operations.
Step 1: Set Up DynamoDB Table
1. Go to the AWS Management Console.
2. Create a DynamoDB table named Products with the primary key:
- Partition Key: id (String).
Step 2: Add Dependencies
Add the following dependencies to your pom.xml:
<!-- DynamoDB SDK -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
</dependency>
<!-- AWS DynamoDB Data Model -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb-enhanced</artifactId>
</dependency>
Step 3: Configuration Class
Create a configuration class to initialize the DynamoDB client and table schema.
package com.example.dynamodb.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder;
import software.amazon.awssdk.services.dynamodb.model.*;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
@Configuration
public class DynamoDbConfig {
@Bean
public DynamoDbClient dynamoDbClient() {
return DynamoDbClient.create();
}
@Bean
public DynamoDbEnhancedClient enhancedClient(DynamoDbClient dynamoDbClient) {
return DynamoDbEnhancedClient.builder()
.dynamoDbClient(dynamoDbClient)
.build();
}
}
Step 4: Create Entity and Repository
Entity Class:
package com.example.dynamodb.model;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.*;
@DynamoDbBean
public class Product {
private String id;
private String name;
private double price;
@DynamoDbPartitionKey
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
package com.example.dynamodb.repository;
import com.example.dynamodb.model.Product;
import org.springframework.stereotype.Repository;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
@Repository
public class ProductRepository {
private final DynamoDbTable<Product> productTable;
public ProductRepository(DynamoDbEnhancedClient enhancedClient) {
this.productTable = enhancedClient.table("Products", TableSchema.fromBean(Product.class));
}
public void save(Product product) {
productTable.putItem(product);
}
public Product getById(String id) {
return productTable.getItem(r -> r.key(k -> k.partitionValue(id)));
}
public void delete(String id) {
productTable.deleteItem(r -> r.key(k -> k.partitionValue(id)));
}
}
Step 5: Service Layer
package com.example.dynamodb.service;
import com.example.dynamodb.model.Product;
import com.example.dynamodb.repository.ProductRepository;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
private final ProductRepository repository;
public ProductService(ProductRepository repository) {
this.repository = repository;
}
public void createProduct(Product product) {
repository.save(product);
}
public Product getProductById(String id) {
return repository.getById(id);
}
public void deleteProduct(String id) {
repository.delete(id);
}
}
Step 6: Controller
package com.example.dynamodb.controller;
import com.example.dynamodb.model.Product;
import com.example.dynamodb.service.ProductService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/products")
public class ProductController {
private final ProductService service;
public ProductController(ProductService service) {
this.service = service;
}
@PostMapping
public ResponseEntity<Void> createProduct(@RequestBody Product product) {
service.createProduct(product);
return ResponseEntity.ok().build();
}
@GetMapping("/{id}")
public ResponseEntity<Product> getProduct(@PathVariable String id) {
return ResponseEntity.ok(service.getProductById(id));
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteProduct(@PathVariable String id) {
service.deleteProduct(id);
return ResponseEntity.ok().build();
}
}
Step 7: Application Properties
Configure DynamoDB properties in application.properties:
spring.application.name=dynamodb-springboot-demo
server.port=8080
Step 8: Run the Application
1. Start the Spring Boot application.
2. Use tools like Postman or cURL to test the CRUD endpoints:
- POST /products
- GET /products/{id}
- DELETE /products/{id}