Spring Boot with DynamoDB

Amazon DynamoDB is AWS managed key-value and document database that delivers single-digit millisecond performance at any scale. In this article, we will build a simple Spring Boot application to integrate with DynamoDB.

1. Create a Spring Boot project

We can use Spring initializer to generate a project. I’ve used Java 11, Maven and Spring Web as a dependency.

2. Add AWS SDK dependencies

Open the project with your IDE and add AWS SDK for Java bill of materials (BOM) dependency to our POM. This will ensure that the modules you specify use the same version of the SDK and that they’re compatible with each other.

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>${aws.java.sdk.bom}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Then, add AWS DynamoDB dependency to our POM

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
</dependency>

3. Configure AWS DynamoDB client and DynamoDB mapper

AmazonDynamoDB provides access to Dynamodb using your credentials. In this case, I have used access key and access secret to interact with DynamoDB. You have to make sure your user has granted DynamoDB permission.

DynamoDBMapper provides interactions with dynamoDB. It loads the data model for dynamodb using annotations defined in com.amazonaws.services.dynamodbv2.datamodeling package. This is much similar to JPA annotations if you are familiar with relational databases such as MySQL.

init() method automatically creates the table in dynamoDB if the table does not exist in AWS.

4. Create a data model

It is time to define our entity class, this is similar to define a POJO entity class in JPA. We can leverage the annotations defined in com.amazonaws.services.dynamodbv2.datamodeling package to map our entity class to a database table. I have defined a simple entity Customer for our application.

5. Create a Dao class

Let's define a data access class to interact with our customer DynamoDB table. I have omitted service layer purposely as we do not have any business logic to process. Also, I have declared only two simple createCustomer and getCustomer methods in the CustomerDao class.

6. Create a rest controller

This simple rest controller has two endpoints to create a customer and retrieve a customer by customerId. Those methods also have basic exception handling as well.

Now everything is ready, we can test our application. Run SpringDynamodbApplication.java class. Once the server is up on http://localhost:5555, you can log in to AWS console and go to DynamoDB service. You should see the created table,

Test 1

Create a customer running following curl command

curl --location --request POST 'http://localhost:5555/customer' \
--header 'Content-Type: application/json' \
--data-raw '{
"name":"Sinol",
"email":"sinol@codersinol.com"
}'

The response of the POST request will be similar to this,

{
"customerId": "86eb9f2d-5d6c-4e5d-8368-4949bceb35b5",
"name": "Sinol",
"email": "sinol@codersinol.com"
}

Test 2

Retrieve the customer calling getCustomer endpoint,

curl --location --request GET 'http://localhost:5555/customers/86eb9f2d-5d6c-4e5d-8368-4949bceb35b5' \
--header 'Content-Type: application/json' \
--data-raw '{
"name":"Sinol",
"email":"sinol@codersinol.com"
}'

You can access full code of this application here https://github.com/coderSinol/spingboot-with-dynamoDB

--

--

--

Love coding and share experience with others

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Customized Tensorflow for macOS

Introducing BPM

Quick fixes: grouping Sonos speakers without the app : Gadget Game News

Advantages of Earning VMware 1V0-41.20 Certification

https://www.vmexam.com/vmware/1v0-41-20-associate-vmware-network-virtualization

iExec V6: Enclaves in Production Mode & Confidential Computing Upgrades

CS371p Fall 2021 blog 10: Zein Fakhreddine

DevOps automation using Jenkins DSL (groovy script)

Structuring Unit Tests in Python

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
Supun Bhagya

Supun Bhagya

Love coding and share experience with others

More from Medium

Deploy Spring Boot Application to Elastic Beanstalk with Github Actions

Validating MongoDB Documents in Spring Boot

A MongoDB collection with a basic JSON schema

Spring Boot — Serverless AWS Lambda With API Gateway

Setup a Kafka broker on Docker