-1

Java: 20.0

Springboot: 3.0.1

Dependency

<dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>aws-sdk-java</artifactId>
      <version>2.20.115</version>
      <scope>provided</scope>
 </dependency>

Service class

@Slf4j
@Service
public class DynamodbClient {

  private final DynamoDbClient dynamoDbClient;
  
  @Value("${amazon.aws.dynamodb.endpoint}")
  private String endpoint;

  @Value("${amazon.aws.dynamodb.region}")
  private String region;

  public DynamodbClient() {
    this.dynamoDbClient =
        DynamoDbClient.builder()
            .endpointOverride(URI.create(endpoint))
            .region(Region.of(region))
            .build();
  }
}

Note: Auth credential is not required. dynamodb is accessible through cli.

Context: Main database is Cassandra for this application and actual requirement should be implemented through an API call, but for some reason, we are not doing it, instead updating a record in dynamodb directly and it is once a while operation.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105

1 Answers1

1

The class DynamodbClient should have a @Configuration annotation instead of @Service. In order to configure your bean definition correctly, you will have to use this annotation. Also, instance variable DynamoDbClient is not required.

The final code will be:

@Configuration
public class DynamodbClient {
  
  @Value("${amazon.aws.dynamodb.endpoint}")
  private String endpoint;

  @Value("${amazon.aws.dynamodb.region}")
  private String region;

  public DynamodbClient() {
    this.dynamoDbClient =
        DynamoDbClient.builder()
            .endpointOverride(URI.create(endpoint))
            .region(Region.of(region))
            .build();
  }
}

@Configuration is a class-level annotation indicating that an object is a source of bean definitions.

Please see this Spring documentation for more details.

SwathiP
  • 315
  • 3
  • 5
  • Thank you for your kind response, I will try this and update you tonight. – Prafulla Kumar Sahu Aug 12 '23 at 14:13
  • I did not try you changes as my code started working and I wanted to use this as an api client, so that it will be replaced with a proper api in future. it was a permission issue for me. It was working earlier, some permission changes impacted it and now after reverting that, it started working. – Prafulla Kumar Sahu Aug 16 '23 at 05:27