本文介绍了为枚举构建通用 DynamoDBMarshalling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 DynamoDB 使用 Amazon Web Services SDK for Java;试图满足@DynamoDBMarshalling 的接口:

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

Class<? extends DynamoDBMarshaller<? extends Object>> marshallerClass();

我构建了一个接收任何枚举的编组器:

I build a marshaller that receives any Enum:

public class EnumMarshaller<T extends Enum<T>> implements DynamoDBMarshaller<T>
{
    @Override
    public String marshall(T getterReturnResult)
    {
        return getterReturnResult.toString();
    }

    @Override
    public T unmarshall(Class<T> clazz, String obj)
    {
        return Enum.valueOf(clazz, obj);
    }
}

问题是,当我尝试在我的字段上使用注释时,我无法弄清楚:

The problem is that when I'm trying to use the annotation on my field I can't figure it out:

public static enum MyEnum {BLA, BLA2}

@DynamoDBMarshalling(marshallerClass=EnumMarshaller<MyEnum>.class)
    public MyEnum getStatus()
    {
        return status;
    }

我无法在泛型类型上调用 .class 并且其他一些尝试出现了不同的错误.我开始认为亚马逊的合同是不可能的...

I can't call .class on a generic type and some other tries came up different errors.I'm starting to think it's impossible with Amazon's contract...

推荐答案

以下对我来说效果很好:

The following worked fine to me:

马歇尔:

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);
    }
}

在我的带有枚举的表类中:

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

这篇关于为枚举构建通用 DynamoDBMarshalling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:42