Issue
I am trying to implement a simple java event-handler lambda for AWS. It receives sqs events and should make appropriate updates to the dynamoDB table.
One of the attributes in this table is a status field that has 4 defined states; therefore I wanted to use an enum class in java and map it to this attribute.
Under AWS SDK v1 I could use the @DynamoDBTypeConvertedEnum annotation. But it does not exist anymore in v2. Instead, there is the @DynamoDbConvertedBy() which receives a converter class reference. There is also an EnumAttributeConverter class which should work nicely with it.
But for some reason, it does not work. The following is a snip from my current code:
@Data
@DynamoDbBean
@NoArgsConstructor
public class Task{
@Getter(onMethod_ = {@DynamoDbPartitionKey})
String id;
...
@Getter(onMethod_ = {@DynamoDbConvertedBy(EnumAttributeConverter.class)})
ExportTaskStatus status;
}
The enum looks as follows:
@RequiredArgsConstructor
public enum TaskStatus {
@JsonProperty("running") PROCESSING(1),
@JsonProperty("succeeded") COMPLETED(2),
@JsonProperty("cancelled") CANCELED(3),
@JsonProperty("failed") FAILED(4);
private final int order;
}
With this, I get the following exception when launching the application:
Class 'class software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.EnumAttributeConverter' appears to have no default constructor thus cannot be used with the BeanTableSchema
Solution
Solution based on watkinsmatthewp Answer:
public class TaskStatusConverter implements AttributeConverter<TaskStatus> {
@Delegate
private final EnumAttributeConverter<TaskStatus> converter;
public TaskStatusConverter() {
converter = EnumAttributeConverter.create(TaskStatus.class);
}
}
Task status attribute looks like this:
@Getter(onMethod_ = {@DynamoDbConvertedBy(TaskStatusConverter.class)})
TaskStatus status;
Answered By - 3Fish
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.