Spring Boot CRUD with AWS DynamoDB

Welcome, in this tutorial, we will see how to configure a spring boot application to use the localhost DynamoDB instance using the spring data. As the DynamoDb AWS service incur changes after frequent HTTP requests so in this tutorial we will use the dynamodb-local on docker (provided by AWS) for learning purpose.

1. Introduction

Before going further in this tutorial, we will look at the common terminology such as introduction to Spring Boot, DynamoDb, and Lombok.

1.1 Spring Boot

  • Spring boot is a module that provides rapid application development feature to the spring framework including auto-configuration, standalone-code, and production-ready code
  • It creates applications that are packaged as jar and are directly started using embedded servlet container (such as Tomcat, Jetty or, Undertow). Thus, no need to deploy the war files
  • It simplifies the maven configuration by providing the starter template and helps to resolve the dependency conflicts. It automatically identifies the required dependencies and imports them into the application
  • It helps in removing the boilerplate code, extra annotations, and XML configurations
  • It provides powerful batch processing and manages the rest endpoints
  • It provides an efficient JPA-starter library to effectively connect the application with the relational databases
  • It offers a Microservice architecture and cloud configuration that manages all the application related configuration properties in a centralized manner

1.2 DynamoDb

  • DynamoDb is a fast and flexible NoSQL database service for all applications that need consistent, single-digit ms latency
  • It is a fully managed cloud database and supports both document and key-value store
  • It also offers a flexible data model, reliable performance, and automatic scaling of the throughput capacity making it the best fit for applications that have inconsistent data for storage
  • It is similar to other NoSQL databases like Cassandra or MongoDB

1.3 Lombok

  • Lombok is nothing but a small library that reduces the amount of boilerplate Java code from the project
  • Automatically generates the getters and setters for the object by using the Lombok annotations
  • Hooks in via the Annotation processor API
  • Raw source code is passed to Lombok for code generation before the Java Compiler continues. Thus, produces properly compiled Java code in conjunction with the Java Compiler
  • Under the target/classes folder you can view the compiled class files
  • Can be used with Maven, Gradle IDE, etc.

1.3.1 Lombok features

Feature Details
val Local variables are declared as final
var Mutable local variables
@Slf4J Creates an SLF4J logger
@Cleanup Will call close() on the resource in the finally block
@Getter Creates getter methods for all properties
@Setter Creates setter for all non-final properties
  • Generates implementations of equals(Object other) and hashCode()
  • By default will use all non-static, non-transient properties
  • Can optionally exclude specific properties
  • Generates String of class name, and each field separated by commas
  • Optional parameter to include field names
  • Optional parameter to include a call to the super toString method
  • Generates no-args constructor
  • Will cause compiler error if there are final fields
  • Can optionally force, which will initialize final fields with 0/false/null var – mutable local variables
  • Generates a constructor for all fields that are final or marked @NonNull
  • The constructor will throw a NullPointerException if any @NonNull fields are null val – local variables are declared final
  • Generates a constructor for all properties of the class
  • Any @NotNull properties will have null checks
  • Generates typical boilerplate code for POJOs
  • Combines – @Getter, @Setter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor
  • No constructor is generated if constructors have been explicitly declared
  • Implements the Builder pattern for object creation
  • The immutable variant of @Data
  • All fields are made private and final by default


Let us go ahead with the tutorial implementation but before going any further I’m assuming that you’re aware of the Spring boot basics.

2. Spring Boot CRUD with AWS DynamoDB

2.1 Application Pre-requisite

To start with this Spring Boot CRUD with AWS DynamoDB tutorial, I am hoping you have the dynamodb-local up and running in your localhost environment. For easy setup, I have the dynamodb-local and dynamodb-admin-gui up and running on the Docker environment. You can execute the below script using the docker-compose command to get the dynamodb-local and dynamodb-admin-gui containers running on Docker in minutes. If you’re doing it for the first time the docker image will be downloaded from the docker hub.


    container_name: dynamodb-local-admin-gui
    image: instructure/dynamo-local-admin
      - '8000:8000'
version: '3.7'

If everything goes well the dynamodb-local and dynamodb-admin-gui containers would be started successfully as shown in Fig. 1. You can use the docker ps -a command to confirm that the containers are started successfully. For further information on docker basics, you can navigate to this tutorial.

Spring Boot AWS DynamoDB - admin gui containers on Docker
Fig. 1: Dynamodb and Dynamodb admin gui containers on Docker

2.2 Tools Used for Spring boot application and Project Structure

We are using Eclipse Kepler SR2, JDK 8, and Maven. In case you’re confused about where you should create the corresponding files or folder, let us review the project structure of the spring boot application.

Spring Boot AWS DynamoDB - Project structure
Fig. 2: Project structure

Let us start building the application!

3. Create a table in Dynamodb

Once the vault server is up and running, head over to the administration console by typing the following address in the browser – http://localhost:8080/. The administration console will open having the Create table button. Click on the button and enter the details as shown in Fig. 3.

Fig. 3: Enter table details

Once done click on the Submit button. If everything goes well the table (named – books) would be created as shown in Fig. 4.

Fig. 4: Table created

4. Creating a Spring Boot application

Below are the steps involved in developing the application.

4.1 Maven Dependency

Here, we specify the dependency for the Spring boot (Web), Java Faker, AWS Dynamodb SDK, Lombok, and Spring Data JPA (to perform the crud operations). Maven will automatically resolve the other dependencies. The updated file will have the following code.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi=""
        <relativePath/> <!-- lookup parent from repository -->
    <description>Springboot and Dynamodb</description>



        <!-- aws-java-dynamodb-sdk -->
        <!-- spring-data-dynamodb-support -->
        <!-- java-faker -->




4.2 Application YML file

Create a new yml file at the location: SpringbootandDynamodb/src/main/resources/ and add the following code to it. Here we will define the application and aws dynamodb configuration. In this tutorial as we’re using the localhost instance of dynamodb so, we will pass the static information but in the real world, this would be replaced with the actual AWS configuration.


    accesskey: key
    region: us-east-1
    secretkey: ''
    endpoint: 'http://localhost:8000/'
  port: 9500
    name: springboot-aws-dynamodb

4.3 Java Classes

Let us write the important java class(es) involved in this application. For brevity, we will skip the following classes –

  • – Bootstrap class to populate dummy data to the dynamodb table (named – books) once the application is started successfully. You can use the get-all-books endpoint to fetch all the books
  • – Service class that provide some business facilities and interact with the DAO layer methods
  • – Exception class to return the 404 status code if an item does not exist in the database
  • – Model class used in the controller to map the incoming request body attributes during the POST and PUT operations

4.3.1 Implementation/Main class

Add the following code to the main class to bootstrap the application from the main method. Always remember, the entry point of the spring boot application is the class containing @SpringBootApplication annotation and the static main method.

package com.springboot.dynamodb;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//lombok annotation for logger
//spring annotation
public class SpringbootandDynamodbApplication {

    public static void main(String[] args) {, args);"Springboot and dynamodb application started successfully.");

4.3.2 Configuration class

Add the following code to the configuration class. The class will be annotated with the @EnableDynamoDBRepositories and will contain the @Bean annotated methods to create the AmazonDynamodbDB instance.

package com.springboot.dynamodb.config;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.github.javafaker.Faker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Locale;

//annotation enables the dynamodb repositories
@EnableDynamoDBRepositories(basePackages = "com.springboot.dynamodb.repo")
public class BeanConfig {

    String endpoint;
    String accesskey;
    String secretkey;
    String region;

    public AwsClientBuilder.EndpointConfiguration endpointConfiguration() {
        return new AwsClientBuilder.EndpointConfiguration(endpoint, region);

    public AWSCredentialsProvider awsCredentialsProvider() {
        return new AWSStaticCredentialsProvider(new BasicAWSCredentials(accesskey, secretkey));

    public AmazonDynamoDB amazonDynamoDB() {
        return AmazonDynamoDBClientBuilder

    public Faker faker() {
        return new Faker(new Locale("en-US"));

4.3.3 Entity class

Add the following code to the model class that will be stored in the dynamodb.

package com.springboot.dynamodb.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

//annotation validates if the dynamodb table exists or not
//if not throws 'ResourceNotFoundException'
//note - dynamodb does not create collection automatically like mongodb so it
//is important to create dynamodb before hand
@DynamoDBTable(tableName = "books")
//lombok annotations
//annotation helps to generate toString(), equals(), hashcode(), getter(), setter()
//annotation helps to generate a no-argument constructor
//annotation helps to generate a constructor with 1 parameter for each field in the class
//annotation helps to implement the builder design pattern
//usage can be seen in
//spring stereotype annotation
public class Book {

	//annotation for marking the property as the hashkey
	@DynamoDBHashKey(attributeName = "id")
	//annotation for making the hashkey property to autogenerate
	//the key. supports string datatype only
	String id;
	//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
	String title;
	String author;
	String genre;
	String publisher;
	int quantity;

4.3.4 Repository interface

Add the following code to the repository interface to define the SQL CRUD functionality.

package com.springboot.dynamodb.repo;

import com.springboot.dynamodb.entity.Book;
import org.springframework.stereotype.Repository;

import java.util.List;

//annotation enables the scan operations
//spring annotation
public interface BookRepository extends CrudRepository<Book, String> {

    long countByGenre(String genre);

    List<Book> findAllByGenre(String genre);

4.3.5 Controller class

Add the following code to the controller class. The class is injected with the service dependency whose method will call the DAO layer methods to persist the data into the database or fetch from it.

package com.springboot.dynamodb.controller;

import com.springboot.dynamodb.entity.Book;
import com.springboot.dynamodb.entity.BookDto;
import com.springboot.dynamodb.exception.EntityNotFound;
import com.springboot.dynamodb.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//lombok annotation for logger
//spring annotations
public class BookController {

    BookService service;

    // HTTP GET URL - http://localhost:9500/api/books
    public List<Book> getBooks() {"Getting all books from the db");
        return service.getBooks();

    // HTTP GET URL - http://localhost:9500/api/books/<book_genre>
    public List<Book> getBooksByGenre(@PathVariable("genre") final String genre) {"Getting books by genre = {} from the db", genre);
        return service.getBooksByGenre(genre);

    // HTTP GET URL - http://localhost:9500/api/book/<book_id>
    public Book getBookById(@PathVariable("id") final String id) throws EntityNotFound {"Getting book id = {} from the db", id);
        return service.getBookById(id);

    // HTTP DELETE URL - http://localhost:9500/api/book/<book_id>
    public void deleteBook(@PathVariable("id") final String id) throws EntityNotFound {"Delete book id = {} from the db", id);

    // HTTP PUT URL  - http://localhost:9500/api/book/<book_id>
    // Sample request body
        "author": "J. K. Rowling",
        "genre": "Fantasy Fiction",
        "publisher": "Bloomsbury Publishing",
        "title": "Harry Potter",
        "quantity": 100
    public void update(@PathVariable("id") final String id, @RequestBody final BookDto dto)
            throws EntityNotFound {"Updating book id = {} into the db", id);
        service.update(id, dto);

    // HTTP POST URL  - http://localhost:9500/api/book
    // Sample request body
        "author": "Vasdev Mohi",
        "genre": "Ghazals",
        "publisher": "Central Sahitya Akademi",
        "title": "Cheque book",
        "quantity": 5
    public void save(@RequestBody final BookDto dto) {"Saving new book = {} into the db", dto.toString());;

    // HTTP POST URL - http://localhost:9500/api/books/count/<book_genre>
    public long getCountByGenre(@PathVariable("genre") final String genre) {
        return service.getCountByGenre(genre);

5. Run the Application

To execute the application, right-click on the class, Run As -> Java Application.

Spring Boot AWS DynamoDB - Run the Application
Fig. 5: Run the Application

6. Project Demo

When the application is started, open the Postman tool to hit the application endpoints to persist the data into the database or fetch from it. You are free to use any other tool of your choice to make the post and get requests to the endpoints.

Application endpoints

-- HTTP GET endpoint (to fetch all the books) –

-- HTTP GET endpoint (to fetch all books by genre) --

-- HTTP GET endpoint (to fetch book by id) --

-- HTTP DELETE endpoint (to delete book by id) –

-- HTTP PUT endpoint (to update an existing book into the database) –

-- sample request body –
        "author": "Harry Potter",
        "genre": "Fantasy Fiction",
        "publisher": "Bloomsbury Publishing",
        "title": "J. K. Rowling",
        "quantity": 1,

-- HTTP POST endpoint (to save a new book into the database) –

-- sample request body –
        "author": "Vasdev Mohi",
        "genre": "Ghazals",
        "publisher": "Central Sahitya Akademi",
        "title": "Cheque book",
        "quantity": 1,

-- HTTP GET endpoint (to fetch books count by genre) --

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

7. Summary

In this section, you learned,

  • Spring boot, Lombok, and Dynamodb introduction
  • Steps to setup dynamodb and dynamodb-admin-gui on docker
  • Steps to create a dynamodb table through admin-gui
  • Steps to configure dynamodb in a spring-boot application through spring data

You can download the sample application as an Eclipse project in the Downloads section.

8. Download the Project

This was an example of how to configure Dynamodb in a Spring Boot application.

You can download the full source code of this example here: Spring Boot CRUD with AWS DynamoDB


An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Srihari Sridharan
Srihari Sridharan
2 years ago


The dynamodb admin GUI is running in port http://localhost:8000/, it is stated as http://localhost:8080/ in Section 3. Kindly confirm. Thanks.

Back to top button