Spring Boot API CRUD with AWS DynamoDB
In this story, we’ll implement a REST API with CRUD operations using Spring boot and AWS DynamoDB.
What is Amazon DynamoDB ?
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. DynamoDB lets you offload the administrative burdens of operating and scaling a distributed database so that you don’t have to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling. DynamoDB also offers encryption at rest, which eliminates the operational burden and complexity involved in protecting sensitive data.
Setting Up DynamoDB in AWS Console
- Login to the AWS Management Console and open the DynamoDB service.
- Create Table. Add the table name and the primary key. (For this story we are using all other default settings)
Our “employees” table has been created.
3. Create a IAM user to access the DynamoDB table structure. We need to access DynamoDB programmatically using an access key and secret access key.
Spring Boot App Setup
We will start by creating a simple Spring Boot project from start.spring.io, with the following dependencies: Web and Lombok.
To implement the integration with aws, we need to add the aws-java-sdk-dynamodb dependency for DynamoDB in the pom.xml file.
We also used Spring Data DynamoDB to facilitate the use of data access with dynamodb.
The second important step is to add the aws credentials to connect to AWS DynamoDB in the properties file.
# AWS properties
access-key: <YOUR ACCESS KEY>
secret-key: <SECRET ACCESS KEY>
- @EnableDynamoDBRepositories annotation is used to enable DynamoDB repositories. We need to introduce the Spring Data repository base package.
- The dynamoDBMapper bean returns a DynamoDBMapper class that allows you to map your client-side classes to Amazon DynamoDB tables. To use DynamoDBMapper, you define the relationship between items in a DynamoDB table and their corresponding object instances in your code.
- The amazonDynamoDB bean allows you to connect your application to the Dynamodb instance with credentials.
The Data Model
Employee model to represent data stored in DynamoDB.
@DynamoDBTable: Annotation validates if the dynamodb table exists or not. dynamodb does not create collection automatically like mongodb so it is important to create dynamodb before hand.
@DynamoDBHashKey: Annotation for marking a property as the hash key for a modeled class.
@DynamoDBAutoGeneratedKey: Annotation for making the hashkey property to autogenerate the key. supports string datatype only
@DynamoDBAttribute: Annotation for describes the field name as it will be represented in dynamodb table offers the name to be different than the field name of the class
Repository interface Controller class
EmployeeRepository interface to define the CRUD functionality.
Test the REST APIs:
Run the Spring Boot Application.
- Save Employee
2. GET employee by Id
3. GET all employees
The complete source code can be found in my GitHub repository.