-1

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?

7
  • I am guessing your @EnableDynamoDBRepositories is not picking up the TenantRepository try using package based search instead: @EnableDynamoDBRepositories(basePackages="com.foo.bar")
    – J Asgarov
    Commented Jul 10 at 5:55
  • That I have tried already, it doesn't change the error Commented Jul 10 at 6:22
  • what is your import for @Repository?
    – J Asgarov
    Commented Jul 10 at 6:24
  • import org.springframework.stereotype.Repository; Commented Jul 10 at 6:36
  • I would strongly advise against using AWS Java SDK V1, especially with Java 21. Commented Jul 10 at 7:57

1 Answer 1

0

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:

https://medium.com/stepstone-tech/integration-testing-with-spring-boot-3-dynamodb-and-localstack-186e674b17be

https://docs.awspring.io/spring-cloud-aws/docs/3.0.0/reference/html/index.html#spring-cloud-aws-dynamoDb

Following these guideline we can implement a custom layer to any CRUD operation on DynamoDB over SpringBoot 3, Java 21.

Not the answer you're looking for? Browse other questions tagged or ask your own question.