Building a general DynamoDBMarshalling for enums

前端 未结 2 700
醉酒成梦
醉酒成梦 2021-01-04 08:10

I\'m using Amazon Web Services SDK for Java for DynamoDB; trying to suffice the interface for @DynamoDBMarshalling:

Class

        
2条回答
  •  别那么骄傲
    2021-01-04 08:40

    The following worked fine to me:

    The Marshaller:

    public class EnumMarshaller implements DynamoDBMarshaller {
        @Override
        public String marshall(Enum getterReturnResult) {
            return getterReturnResult.name();
        }
    
        @Override
        public Enum unmarshall(Class clazz, String obj) {
            return Enum.valueOf(clazz, obj);
        }
    }
    

    In my table class with an enum:

    @DynamoDBMarshalling(marshallerClass=EnumMarshaller.class)
    @DynamoDBAttribute(attributeName = "MyEnum")
    public MyEnum getMyEnum() {
       return myEnum;
    }
    

提交回复
热议问题