top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Spring Boot REST API using DynamoDB in local

I recently got a chance to work on a project to build a Rest API with Amazon Dynamo DB database. In this article, I am sharing my learning on how to build a Rest API using Spring Boot and AWS DynamoDB in local.





We will follow the following steps :

1) Setup Amazon DynamoDB in local

Amazon DynamoDB is a fully managed, serverless, key-value NoSQL database designed on AWS.


It is available as a downloadable version. You can follow the instructions in this link to setup DynamoDb in local. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html


Once we download and setup the dynamoDB, we can start the dynamoDB from the terminal/command prompt by navigating to the folder where it is setup and executing this command.


java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb


You can follow this link to learn more about Amazon DynamoDB. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html


2) Setup NoSQL workbench


Once you setup the local instance of DynamoDB, install NoSQL workbench.


NoSQL Workbench for Amazon DynamoDB is a cross-platform client-side GUI application for modern database development and operations. We can use NOSQL workbench for accessing the database.


You can learn more about NoSQL workbench from this link and install it. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/workbench.html


3) Create Simple Spring Boot Project


I am using Java 1.8 , Eclipse IDE, Maven for this project.


3.1 Install Spring Plugin from Eclipse Marketplace:


I have used Spring Plugin from Eclipse Marketplace to create a spring boot project.


If you do not have Spring plugin installed on your Eclipse, Search for ‘Spring Tools 4’ on ‘Eclipse Marketplace’ and install it by following the on-screen prompts and accepting the license agreement. You will be asked to restart eclipse once the installation is complete.


Alternatively, you can also create a simple Spring Boot project from start.spring.io with Lombok and Spring Web dependencies and download the jar and import it into eclipse.


Once Eclipse IDE restarts after installing Spring plugin Navigate to : New -> File -> Project -> Spring Boot -> Select Spring Starter Project

Click ‘Next’ and provide a meaningful Name, Artifact and Package details. You can provide a description for your project and click ‘Next’ to choose the required dependencies as shown in the screen shot below.


Add Lombok and Spring Web dependencies in the next screen and ‘Click Finish’ to create a Spring Boot project.

4) Project Structure


The complete Project Structure for REST API with basic CRUD operations on a Student Data Model is as shown below. Let’s explore the classes one by one.



4.1 Maven Dependencies:


To support Amazon Dynamo DB , add the following maven dependencies to pom.xml

  • aws-java-sdk-dynamodb — The AWS Java SDK for Amazon DynamoDB module holds the client classes that are used for communicating with Amazon DynamoDB Service

  • spring-data-dynamodb — Spring Data project is used to to build Spring-powered applications that use data access technologies. This module deals with enhanced support for a data access layer built on AWS DynamoDB.


The entire pom.xml looks like this:



4.2 Application.properties:


Add the following properties to application.properties file under src/main/resources



As we are using a local instance of dynamoDB, the access and secret keys have to be populated with just arbitrary values. The properties will be dynamically pulled out of the application.properties file in the Spring config.


4.3 Configuration class:


Create a config class and add the following code to the class. The class will be annotated with the @EnableDynamoDBRepositories and will contain the @Bean annotated methods to create the AmazonDynamoDB instance.



  • @EnableDynamoDBRepositories annotation is used to enable DynamoDB repositories. We need to add the Spring Data repository base package here. Then DynamoDB repository capabilities will be added to our Spring data repositories.

  • The signingRegion is set as us-west-2, This does not affect while using local DynamoDB. This will be used when we connect to DynamoDB instance created on AWS console.

  • The amazonDynamoDB bean allows us to connect our application to the DynamoDB instance with credentials. Again, This does not affect the local environment. But will be required when connecting to DynamoDB instance on AWS.


4.4 Different Project Classes:


4.4.1 Main class:


The entry point of the spring boot application is the class containing @SpringBootApplication annotation and the static main method.


4.4.2 Entity class:


Let’s define a Student Entity with the required fields.



  • @DynamoDBTable: Annotation validates if the dynamoDB table exists or not.

  • @DynamoDBHashKey: Annotation for marking a property as the hash key for an entity class.

  • @DynamoDBAttribute: Annotation to mark a class property as an attribute in a DynamoDB table. This annotation is optional when the name of the DynamoDB attribute matches the name of the property declared in the class. When they differ, use this annotation with the attributeName() parameter to specify which DynamoDB attribute this property corresponds to.

4.4.3 Service Class:


Adding a service layer (StudentService class) to implement all the business logic for the CRUD operations. Also, included custom exception handling for Entity Not Found scenarios.


4.4.4 Controller Class:


Student Controller class for exposing the Rest API endpoints for CRUD operations. Here, the repository layer is not exposed directly to controller layer. Instead I am calling the Service layer from the controller layer and then the Service layer interacts with the Repository layer.


4.4.5 Repository Interface:



We will use the CrudRepository which allows us to build communication with the database easily.


@EnableScan annotation - the repository implementation will be registered as a component in the Spring container.


6) Create dynamoDB database table:

Open NoSQL Workbench → Click on ‘Amazon Dynamo DB’ and then Click on ‘Create new data model’ in the ‘Getting Started’ section.


Enter the Name as “StudentDataModel” and provide a Description and Author name. And Click Create.


Click on ‘Data Modeler’ and then click on ‘+’ next to tables to add tables.



Enter the Table Name and Primary Key and Other Attributes.

Scroll down and click Add.



A Student table will be created as shown



Go to the ‘Visualizer’ and Click on ‘Commit to Amazon DynamoDB’.


Select the ‘default’ under ‘Use saved connection’ and Click Commit. Now the DynamoDB is ready to use.


7) Run the application


Now, that we have the project ready , We can test the project in Postman.


Run the Spring Boot Application. Right click on the RestApiSpringBootWithDynamoDbApplication.java and Select Run As-> Spring Boot App



Once the tomcat server has started. Open Postman and send the requests to check all the CRUD operations.


8) Test on Postman


Post Request to Add Student:




Get Request to get all students:


Get Request to get a student by id:



Put Request to update a student by id


Delete Request to delete a student:



All done. We successfully connected dynamoDB from a Spring Boot Rest API Application and performed basic CRUD operations.


The source code can be found on my GitHub.

https://github.com/vidsdn/Spring-Boot-Rest-API-using-DynamoDB.git



1,046 views2 comments

Recent Posts

See All
bottom of page