Building a general DynamoDBMarshalling for enums

前端 未结 2 701
醉酒成梦
醉酒成梦 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<Enum> {
        @Override
        public String marshall(Enum getterReturnResult) {
            return getterReturnResult.name();
        }
    
        @Override
        public Enum unmarshall(Class<Enum> 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;
    }
    
    0 讨论(0)
  • 2021-01-04 08:41

    I worked around this problem by sub-classing the JsonMarshaller with a specific class type:

    public class FooMarshaller extends JsonMarshaller<Foo>
    {
        // No impl required
    }
    

    Then the marshalling annotation is added to the data property like:

    @DynamoDBMarshalling(marshallerClass = FooMarshaller.class)
    public Set<Foo> getFoos()
    {
        return foos;
    }
    

    A pain if you have to add many class types, but it works.

    0 讨论(0)
提交回复
热议问题