DynamoDb查询枚举

时间:2018-04-04 08:46:53

标签: java amazon-dynamodb dynamodb-queries

我是dynamoDB的新手,这个问题可能是少年。

我正在尝试从表中查询项目,但dynamo api无法将查询json结果转换为类实例,因为类中存在枚举变量成员。

  

我的类结构:[注意RequestStage是一个枚举成员]

@DynamoDBTable(tableName = "LoadInfoProvider")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public final class LoadInfoProvider {

/** The load request ID. */
@DynamoDBRangeKey
private String loadRequestID;

/** The load request creation id. */
@DynamoDBHashKey
private String loadRequestCreationId;

/** The carrier ID. */
private String carrierID;

/** The carrier pool. */
private String carrierPool;

/** The carrier details. */
private CarrierDetail carrierDetails;

/** The driver assignment required. */
private Boolean driverAssignmentRequired;

/** The driver pool. */
private String driverPool;

/** The trailer attachment required. */
private Boolean trailerAttachmentRequired;

/** The request type. */
private String requestType;

/** The request stage. */
private RequestStage requestStage;

/** The created time. */
private String createdTime;

/** The consumed time. */
private String consumedTime;

/** The provider status. */
private String providerStatus;

/** The is consumed. */
private Boolean isConsumed;

/** The request status. */
private RequestStatus requestStatus;

/** The trip. */
private Trip trip;
}
  

RequestStageEnum:

@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PUBLIC)
@DynamoDBDocument
public enum RequestStage {

/** The initiated. */
INITIATED("initiated"),

/** The passed. */
PASSED("passed"),

/** The failed. */
FAILED("failed");

/** The value. */
@Getter
@Setter
private String value;

}

当我从这个表中查询某些内容时,我收到以下错误。

  

线程中的异常" main" com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException:无法实例化新的类实例       在com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas $ StandardItemConverter.createObject(ConversionSchemas.java:614)       在com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas $ StandardItemConverter.unconvert(ConversionSchemas.java:414)       在com.amazonaws.services.dynamodbv2.datamodeling.unmarshallers.ObjectUnmarshaller.unmarshall(ObjectUnmarshaller.java:54)       在com.amazonaws.services.dynamodbv2.datamodeling.unmarshallers.NullableUnmarshaller.unmarshall(NullableUnmarshaller.java:46)       在com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas $ StandardItemConverter.unmarshall(ConversionSchemas.java:593)       在com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas $ StandardItemConverter.unconvert(ConversionSchemas.java:426)       在com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.privateMarshallIntoObject(DynamoDBMapper.java:630)       在com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.marshallIntoObjects(DynamoDBMapper.java:661)       在com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList。(PaginatedQueryList.java:65)       在com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.query(DynamoDBMapper.java:1856)       在com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.query(DynamoDBMapper.java:1844)       在com.amazon.optimus.dynamoHelper.DynamoDBUtil.queryRecord(DynamoDBUtil.java:112)       在com.amazon.optimus.dynamoHelper.DynamoDBUtil.main(DynamoDBUtil.java:164)   引起:java.lang.InstantiationException:com.amazon.optimus.enums.RequestStage       在java.lang.Class.newInstance(Class.java:427)       在com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas $ StandardItemConverter.createObject(ConversionSchemas.java:611)       ......还有12个   引起:java.lang.NoSuchMethodException:com.amazon.optimus.enums.RequestStage。()       在java.lang.Class.getConstructor0(Class.java:3082)       在java.lang.Class.newInstance(Class.java:412)       ......还有13个

从异常中可以清楚地看出,java无法实例化枚举。我如何解决这个问题并使其有效?感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您需要使用requestStage为您的字段@DynamoDBMarshalling添加注释。

假设RequestStageenum,这就是你的编组员的样子:

    public class RequestStageMarshaller implements DynamoDBMarshaller<RequestStage> {

        @Override
        public String marshall(RequestStage requestStage) {
            return requestStage.toString();
        }

        @Override
        public RequestStage unmarshall(Class<RequestStage> clazz, String 
requestStage) {
            return RequestStage.valueOf(requestStage);
        }

    }

然后你会像这样注释你的requestStage字段:

@DynamoDBMarshalling(marshallerClass = RequestStageMarshaller.class)
private RequestStage requestStage