0

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

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

我的班级结构:[注意 RequestStage 是 Enum 成员]

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

请求阶段枚举:

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

}

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

线程“主”com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException 中的异常:无法在 com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.createObject(ConversionSchemas.java:614) 处实例化类的新实例.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 at java.lang.Class.newInstance(Class.java:427) at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.createObject(ConversionSchemas.java:611)。 .. 12 更多原因: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 个getConstructor0(Class.java:3082) at java.lang.Class.newInstance(Class.java:412) ... 13 更多getConstructor0(Class.java:3082) at java.lang.Class.newInstance(Class.java:412) ... 13 更多

从异常中可以清楚地看出,java 无法实例化枚举。我怎样才能解决这个问题并使它工作?感谢您的帮助。

4

1 回答 1

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
于 2018-04-04T11:32:28.637 回答