I am trying to integrate with DynamoDB using Spring boot 3.x. I am using Java 21 and have following configuration in gradle for DynamoDb:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.data:spring-data-commons:2.7.18'
implementation 'com.amazonaws:aws-java-sdk-dynamodb:1.12.714'
implementation 'com.github.derjust:spring-data-dynamodb:5.1.0'
implementation group: 'org.modelmapper', name: 'modelmapper', version: '2.1.1'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
DynamoDBConfig for beans:
@EnableDynamoDBRepositories( includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Repository.class))
public class DynamoDBConfig {
// @Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint = "http://localhost:8000/";
//@Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey = "key";
//@Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey ="dfjkdkf";
@Bean
public AmazonDynamoDB amazonDynamoDB() {
AmazonDynamoDB amazonDynamoDB
= new AmazonDynamoDBClient(amazonAWSCredentials());
if (!StringUtils.isEmpty(amazonDynamoDBEndpoint)) {
amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
}
return amazonDynamoDB;
}
@Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(
amazonAWSAccessKey, amazonAWSSecretKey);
}
}
Entity Class:
@Data
@DynamoDBTable(tableName = "tenant")
public class Tenant {
@DynamoDBHashKey
private String id;
@DynamoDBAttribute
private String name;
@DynamoDBAttribute
private String country;
@DynamoDBAttribute
private Status status;
}
Repository:
@EnableScan
@Repository
public interface TenantRepository extends CrudRepository<Tenant, String> {
}
All these classes are the part of basepackage where I have Application.java class. When I run the application I get an error:
Consider defining a bean of type 'com.middleware.TenantRepository' in your configuration
Am I missing some configuration for DynamoDB with SPringBoot (JPA).
UPDATE To Question:
Updated the AWS SDK to
implementation 'software.amazon.awssdk:bom:2.21.1'
implementation 'software.amazon.awssdk:dynamodb-enhanced:2.20.87'
Yet, the JPA repository is not found. Is JPA compatible with DynamoDB with new SDK and java 21? What are the recommended methods to connect with DynamoDB and read/write data to it using Spring boot 3.x, JPA and Java 21?
Spring Data doesn't offer a direct integration with the AWS SDK v2 for interacting with DynamoDB at the moment. Fallback option is to use a custom implementation to interact with DynamoDB. Here are some samples:
Following these guideline we can implement a custom layer to any CRUD operation on DynamoDB over SpringBoot 3, Java 21.