Spring Boot API CRUD with AWS DynamoDB

Photo by Kyle Glenn on Unsplash

What is Amazon DynamoDB ?

Setting Up DynamoDB in AWS Console

  1. Login to the AWS Management Console and open the DynamoDB service.
  2. Create Table. Add the table name and the primary key. (For this story we are using all other default settings)

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.

Maven Dependency

To implement the integration with aws, we need to add the aws-java-sdk-dynamodb dependency for DynamoDB in the pom.xml file.

<dependency>
<
groupId>com.amazonaws</groupId>
<
artifactId>aws-java-sdk-dynamodb</artifactId>
<
version>1.12.85</version>
</
dependency>
<dependency>
<
groupId>com.github.derjust</groupId>
<
artifactId>spring-data-dynamodb</artifactId>
<
version>5.0.4</version>
</
dependency>

Application Properties

The second important step is to add the aws credentials to connect to AWS DynamoDB in the properties file.

# AWS properties
aws:
access-key: <YOUR ACCESS KEY>
secret-key: <SECRET ACCESS KEY>
region: us-east-2
endpoint: dynamodb.us-east-2.amazonaws.com

Project Structure

DynamoDB Configuration

  • @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.
    dynamo-user

The Data Model

Employee model to represent data stored in DynamoDB.

Repository interface Controller class

Controller class

Test the REST APIs:

Run the Spring Boot Application.

  1. Save Employee
AWS DynamoDB Console

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store