How to use Spring Data DynamoDB example | Spring Boot

In tradition approach, implementing Data Access Layer makes lots of boilerplate code. Spring Data helps us improve our codes and reduce efforts for development and maintenance. It supports us the ways to write interface for repositories and custom finder methods, the implementation will be done automatically by Spring Framework. In this tutorial, we’re gonna look at how to use Spring Data DynamoDB with Spring Boot.

Related Posts:
Spring MongoOperations to access MongoDB
How to build SpringBoot MongoDb RestfulApi
How to use SpringData MongoRepository to interact with MongoDB

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.9.0.RELEASE
– Spring Boot: 1.5.9.RELEASE

II. Overview
1. Goal

spring-data-dynamodb-goal

2. Project Structure

spring-data-dynamodb-structure

– Class Customer corresponds to entity and table Customer.
CustomerRepository is an interface extends CrudRepository, will be autowired in WebController for implementing repository methods and custom finder methods.
WebController is a REST Controller which has request mapping methods for RESTful requests such as: save, delete, findall, findbyid, findbylastname.
– Configuration for DynamoDB properties in application.properties. The accesskey and secretkey are just arbitrary values and are not needed to actually authenticate when accessing local instance of DynamoDB. The properties will be dynamically pulled out in the DynamoDBConfig.
Dependencies for Spring Boot and DynamoDB in pom.xml

III. Practice
1. Create Spring Boot project & add Dependencies

2. Configure Spring Data DynamoDB

Open application.properties:

Under package config, create DynamoDBConfig class:

3. Create DataModel Class

Under package model, create Customer class:

4. Create Repository Interface

This interface helps us do all CRUD functions:

5. Create Web Controller

The controller receives requests from client, using repository to add/get data and return results:

6. Create DynamoDB table

– Run DynamoDB:

– Create Customer Table:

Response is like:

7. Run Spring Boot Application & Check Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Request 1
http://localhost:8080/save
The browser returns Done and if checking database with Customer table, we can see some data rows has been added:
spring-data-dynamodb-table-result-save

Request 2
http://localhost:8080/findall
spring-data-dynamodb-table-result-findall

Request 3
http://localhost:8080/findbyid?id=JSA-3
spring-data-dynamodb-table-result-findbyid

Request 4
http://localhost:8080/findbylastname?lastname=Smith
spring-data-dynamodb-table-result-findbylastname

Request 5
http://localhost:8080/delete
The browser returns Done and if checking database with Customer table, we can see:
spring-data-dynamodb-table-result-delete

8. Way to check DynamoDB Table

Go to http://localhost:8000/shell, then run the code below:

IV. Source Code

SpringDataDynamoDB

By JavaSampleApproach | December 29, 2017.

Related Posts


3 thoughts on “How to use Spring Data DynamoDB example | Spring Boot”

  1. I’m trying to setup a class that has both the @DynamoDBHashKey and a @DynamoDBRangeKey, but I get a spring error when I define @DynamoDBRangeKey. If I remove the @DynamoDBRangeKey annotation from the field then Spring doesn’t complain, but I need both types of keys.

    This is the error I get with both key annotations defined:
    Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Couldn’t find field with annotation interface org.springframework.data.annotation.Id!

    Any ideas what would be causing this?

Got Something To Say:

Your email address will not be published. Required fields are marked *

*