gpt4 book ai didi

java - DynamoDb 查询枚举

转载 作者:行者123 更新时间:2023-12-02 11:25:38 25 4
gpt4 key购买 nike

我是 dynamoDB 的新手,这个问题可能还很幼稚。

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

My class Structure:[note that RequestStage is a Enum member]

@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;

}

当我从此表中查询某些内容时,出现以下错误。

Exception in thread "main" com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Failed to instantiate new instance of class at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.createObject(ConversionSchemas.java:614) at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.unconvert(ConversionSchemas.java:414) at com.amazonaws.services.dynamodbv2.datamodeling.unmarshallers.ObjectUnmarshaller.unmarshall(ObjectUnmarshaller.java:54) at com.amazonaws.services.dynamodbv2.datamodeling.unmarshallers.NullableUnmarshaller.unmarshall(NullableUnmarshaller.java:46) at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.unmarshall(ConversionSchemas.java:593) at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.unconvert(ConversionSchemas.java:426) at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.privateMarshallIntoObject(DynamoDBMapper.java:630) at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.marshallIntoObjects(DynamoDBMapper.java:661) at com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList.(PaginatedQueryList.java:65) at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.query(DynamoDBMapper.java:1856) at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.query(DynamoDBMapper.java:1844) at com.amazon.optimus.dynamoHelper.DynamoDBUtil.queryRecord(DynamoDBUtil.java:112) at com.amazon.optimus.dynamoHelper.DynamoDBUtil.main(DynamoDBUtil.java:164) Caused by: java.lang.InstantiationException: com.amazon.optimus.enums.RequestStage at java.lang.Class.newInstance(Class.java:427) at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.createObject(ConversionSchemas.java:611) ... 12 more Caused by: java.lang.NoSuchMethodException: com.amazon.optimus.enums.RequestStage.() at java.lang.Class.getConstructor0(Class.java:3082) at java.lang.Class.newInstance(Class.java:412) ... 13 more

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

最佳答案

您需要使用 @DynamoDBMarshalling 注释您的字段 requestStage

假设 RequestStage 是一个 enum,您的编码器可能如下所示:

    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

关于java - DynamoDb 查询枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com