How to correctly use DynamoDB with Spring?
I tried to follow many tutorials but always got the same error.
For example my realization of [baeldung tutorial][1]:
#AWS-creds
amazon.dynamodb.endpoint=https://dynamodb.us-east-1.amazonaws.com
amazon.aws.accesskey=
amazon.aws.secretkey=
spring.jpa.hibernate.ddl-auto=create
spring.main.allow-bean-definition-overriding=true
@Configuration
@EnableDynamoDBRepositories
(basePackages = "com.hrzc.demo.repository")
public class DynamoDBConfig {
@Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint;
@Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
@Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
@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);
}
}
@DynamoDBTable(tableName = "Url")
public class Url {
private int id;
private String longUrl;
@DynamoDBAttribute
@DynamoDBRangeKey
public String getLongUrl() {
return longUrl;
}
public void setLongUrl(String longUrl) {
this.longUrl = longUrl;
}
@DynamoDBHashKey
@DynamoDBAutoGeneratedKey
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@EnableScan
public interface UrlRepository extends CrudRepository<Url, Long> {
}
The pom
file does not equal the pom
from the tutorial, but I hope this is not the reason, since I can't start the project with dependencyManagement
- the error sounds like An attempt was made to call a method that does not exist.
- and I found the suggestion to remove the spring-boot-data overriding
[here][2].
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hrzc</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>linkshrtnr</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.64</version>
</dependency>
<dependency>
<groupId>com.github.derjust</groupId>
<artifactId>spring-data-dynamodb</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Url
table via aws-cli
:> aws dynamodb describe-table --table-name Url
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "N"
},
{
"AttributeName": "longUrl",
"AttributeType": "S"
}
],
"TableName": "Url",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
},
{
"AttributeName": "longUrl",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": "2022-09-25T22:40:39.385000+03:00",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:us-east-1:393546347656:table/Url",
"TableId": "33f8baed-4d6c-4787-845d-1df7d0749cea"
}
}
So, how to use dynamoDB correctly?
[1]: https://www.baeldung.com/spring-data-dynamodb
[2]: https://stackoverflow.com/questions/64171751/spring-boot-refuses-to-start-problem-with-abstractrepositoryconfigurationsource
check your application properties file
#for mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#for postgres
#spring.datasource.driver-class-name=org.postgresql.Driver
my properties file is with other config is like
spring.datasource.name=localSource
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL8Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
sometimes also check to update maven dependency by updating the maven (sts4/ eclipse)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.