Tomás Juárez
Tomás Juárez

Reputation: 1514

ValidationException on DynamoDB with AWS sdk for Java

I'm trying to put an item and get it back a few statements later, but I'm getting a cryptic error from AWS.

class DataStore<Payload extends EventPayload> {   
    private final Clock clock;

    private final Table table;

    private final ObjectMapper objectMapper = new ObjectMapper();

    private final Class<Payload> payloadType;

    public DynamoDBEventStore(final Clock clock, final Class<Payload> eventClass, final String dataStoreName) {
        final AmazonDynamoDB client =
                AmazonDynamoDBClientBuilder.standard().build();

        final DynamoDB dynamoDB = new DynamoDB(client);

        this.table = dynamoDB.getTable(dataStoreName);
        this.clock = clock;
        this.payloadType = eventClass;
    }

    public void persist(final UUID eventId, final String aggregateId, final Long version, final Payload eventPayload) {
        final Map<String, Object> rawDomainEvent = Map.of(
                "EventId", eventId.toString(),
                "Timestamp", LocalDateTime.now(clock).toString(),
                "AggregateId", eventPayload.getAggregateKey(),
                "Version", version,
                "Payload", objectMapper.convertValue(eventPayload, Map.class)
        );
    
        final Item domainEvent = Item.fromMap(rawDomainEvent);
        table.putItem(domainEvent);
     }
    
        
    public void testEvent(final UUID eventId) {
        table.getItem("EventId", eventId.toString();
    }
}

If I save an item calling to persist, then the Item is saved as expected (see the JSON below from DynamoDB console).

 {
      "EventId": {
        "S": "8a2c1733-887d-42e1-b720-2e87dcf46269"
      },
      "Timestamp": {
        "S": "2021-10-10T04:18:56.465223700"
      },
      "Payload": {
        "M": {
          "transactions": {
            "L": []
          },
          "aggregateKey": {
            "S": "bc406432-37f1-440f-9157-b0ce8da814c1"
          }
        }
      },
      "Version": {
        "N": "1"
      },
      "AggregateId": {
        "S": "bc406432-37f1-440f-9157-b0ce8da814c1"
      }
    }

But when I call to testEvent it fails returning the following error:

  "message": "Unable to unmarshall exception response with the unmarshallers provided (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: E6M3NI8CTF05ONUC5G25H53PT7VV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)",

Any thoughts about what could be wrong with my code? FYI, I'm using Java 11, Spring Boot 2.5.4 and AWS SDK 1.12.81:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-dynamodb</artifactId>
    <version>1.12.81</version>
</dependency>

Upvotes: 3

Views: 7959

Answers (1)

Felix
Felix

Reputation: 10078

The error message isn't that cryptic! Ignore the unmarshalling part, and you are left with

Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; 

I would guess, your eventId can't be found in the database - but you need to debug from here

Upvotes: 3

Related Questions