Vino
Vino

Reputation: 2311

Setting up DynamoDB with SpringBoot

I am trying to setup local DynamoDB instance with SpringBoot. I am following this but with Gradle.

When I try to run my application I get the following exception:

BeanCreationException: Error creating bean with name 'agentRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

I do understand that this is a failure to dependency inject due to ambiguity but my AgentRepository as a no-arg constructor. Not sure where the ambiguity is.

The following is my code:

Gradle file

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'test.project'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/milestone" }
}

ext['springCloudVersion'] = 'Greenwich.M3'

dependencies {
    //implementation('org.springframework.boot:spring-boot-starter-data-redis')
    implementation('org.springframework.boot:spring-boot-starter-web')

    compile 'org.springframework.data:spring-data-releasetrain:Hopper-SR10'
    compile 'com.amazonaws:aws-java-sdk-dynamodb:1.11.34'
    compile 'com.github.derjust:spring-data-dynamodb:4.3.1'

    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

DynamoDBConfig

@Configuration
@EnableDynamoDBRepositories(basePackages = "test.project.agent.agent")
public class DynamoConfig
{
    @Value("${aws.dynamodb.endpoint}")
    private String dynamoDBEndpoint;

    @Value("${aws.auth.accesskey}")
    private String awsAccessKey;

    @Value("${aws.auth.secretkey}")
    private String awsSecretKey;

    @Bean
    public AmazonDynamoDB amazonDynamoDB()
    {
        AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(getAwsCredentials());
        dynamoDB.setEndpoint(dynamoDBEndpoint);

        return dynamoDB;
    }

    @Bean
    public AWSCredentials getAwsCredentials()
    {
        return new BasicAWSCredentials(awsAccessKey, awsSecretKey);
    }
}

Agent (entity)

@DynamoDBTable(tableName = "agent") public class Agent { private String agentNumber; private Integer id; private Business business; private AgentState agentState;

    @DynamoDBHashKey(attributeName = "agentNumber")
    public String getAgentNumber()
    {
        return agentNumber;
    }

    public void setAgentNumber(String agentNumber)
    {
        this.agentNumber = agentNumber;
    }

    @DynamoDBAttribute(attributeName = "id")
    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    @DynamoDBAttribute(attributeName = "business")
    public Business getBusiness()
    {
        return business;
    }

    public void setBusiness(Business business)
    {
        this.business = business;
    }

    @DynamoDBAttribute(attributeName = "agentState")
    public AgentState getAgentState()
    {
        return agentState;
    }

    public void setAgentState(AgentState agentState)
    {
        this.agentState = agentState;
    }
}

AgentRepository

package test.project.agent.agent;

import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.springframework.data.repository.CrudRepository;


@EnableScan
public interface AgentRepository extends CrudRepository<Agent, String>
{
    Agent findByNumber(String number);
}

AgentController (Where AgentRepository is injected)

@RestController
@RequestMapping(value = "/v1/agents")
public class AgentController
{
    @Autowired
    private AgentRepository agentRepository;

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public void test()
    {
        Agent agent = new Agent();
        agent.setAgentNumber("123456");
        agent.setId(1);
    }
}

What's the cause of this error and how can I resolve this?

Note: Even when part where the AgentRepository injected is commented out, the application fails to start.

In addition to the aforementioned error I do get the following exception too:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'methodValidationPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'agentRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

Upvotes: 2

Views: 6025

Answers (2)

smac2020
smac2020

Reputation: 10734

We created an end to end development article that shows HOW TO create a Spring Boot application that interacts with DynamoDB. In this application, the Java V2 DynamoDB API (and other AWS APIS) is used. Plus it shows use of the V2 Enhanced Client - a new feature of the DynamoDB V2 API.

As well, it demonstrates how to deploy the app to Elastic Beanstalk.

enter image description here

You can find the development article here: https://github.com/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_dynamodb_web_app

Upvotes: 0

Vino
Vino

Reputation: 2311

The error message is rather misleading. The actual reason is the spring-data-dynamodb library mismatch with spring-boot version. This page lists the version requirement. So in my case the fix was to use an update spring-data-dynamodb.

So in Gradle file (using version 5.0.4 as opposed to 4.3.1)

compile group: 'com.github.derjust', name: 'spring-data-dynamodb', version: '5.0.4'

Upvotes: 5

Related Questions