使用SpringBoot设置DynamoDB

时间:2018-12-07 11:47:05

标签: java spring-boot amazon-dynamodb

我正在尝试使用SpringBoot设置本地DynamoDB实例。我正在关注this,但正在关注Gradle。

当我尝试运行我的应用程序时,出现以下异常:

  

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)

我确实理解这是由于模棱两可而导致的依赖项注入失败,但是我的AgentRepository作为无参构造函数。不确定歧义在哪里。

以下是我的代码:

成绩文件

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);
    }
}

代理人(实体)

@DynamoDBTable(tableName =“ agent”)     公共类代理     {         私有字符串agentNumber;         私有整数ID;         私人业务;         私有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(在其中注入了AgentRepository的地方)

@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);
    }
}

此错误的原因是什么,我该如何解决?

注意:即使注释掉注入的AgentRepository的部分,应用程序也无法启动。

除了上述错误之外,我也确实获得了以下异常:

  

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)

2 个答案:

答案 0 :(得分:1)

该错误消息颇具误导性。实际原因是"WOLZA","Wolski Zajazd","Zbyszek Piestrzeniewicz","Owner","ul. Filtrowa 68","Warszawa","","01-012","Poland","(26) 642-7012","(26) 642-7012",3694.0000 "WINCA","Wenna Wines","Vladimir Yakovski","Owner","","","","","","","",0.0000 "XXXXXX","Linked Server Company","","","","","","","","","",0.0000 Dumped contents of /Users/cetinBasoz/Desktop/customer.csv Press any key to continue... 库与spring-data-dynamodb版本不匹配。 This页列出了版本要求。因此,对于我来说,解决方法是使用更新spring-boot

因此在Gradle文件中(使用5.0.4版而不是4.3.1版)

spring-data-dynamodb

答案 1 :(得分:0)

我们创建了一条端到端的开发文章,该文章显示了如何创建与DynamoDB交互的Spring Boot应用程序。在此应用程序中,使用了Java V2 DynamoDB API(和其他AWS APIS)。此外,它还展示了V2增强客户端的使用-DynamoDB V2 API的新功能。

此外,它还演示了如何将应用程序部署到Elastic Beanstalk。

enter image description here

您可以在此处找到开发文章:https://github.com/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_dynamodb_web_app