我正在使用Kotlin开发一个Android应用程序。对于后端,我正在使用Dynamo DB和Cognito服务。为了以前在dynamo数据库中创建项目,我使用了如下所示的Document。
fun createItem()
{
thread(start = true){
val credentialsProvider = CognitoCachingCredentialsProvider(
activity, MainApplication.COGNITO_IDENTITY_POOL_ID, MainApplication.AWS_REGION)
val dbClient = AmazonDynamoDBClient(credentialsProvider);
val dbItemTable = Table.loadTable(dbClient, MainApplication.DB_TABLE_ITEMS)
val item: Document = Document()
if (!item.containsKey("Id")) {
item.put("Id", credentialsProvider.getCachedIdentityId() + UUID.randomUUID())
}
if (!item.containsKey("UserId")) {
item.put("UserId", credentialsProvider.getCachedIdentityId())
}
if (!item.containsKey("Name")) {
item.put("Name", contentView.item_tf_name.text.toString())
}
if (!item.containsKey("Description")) {
item.put("Description", contentView.item_tf_description.toString())
}
dbItemTable.putItem(item)
}
}
上面的代码工作正常。但是我想使用DynamoDBMapper并将表绑定到Kotlin类。
所以我创建了一个类,以映射到具有以下定义的表
@DynamoDBTable(tableName = "xxxx-3302xx-item")
class ItemDO {
@get:DynamoDBHashKey(attributeName = "Id")
@get:DynamoDBAttribute(attributeName = "Id")
var id: String? = null
@get:DynamoDBAttribute(attributeName = "Description")
var description: String? = null
@get:DynamoDBAttribute(attributeName = "Name")
var name: String? = null
@get:DynamoDBAttribute(attributeName = "UserId")
var userId: String? = null
}
然后我更改了我的createItem。这就是定义
fun createItem()
{
thread(start = true){
val cognitoCredentialsProvider = CognitoCachingCredentialsProvider(activity, MainApplication.COGNITO_IDENTITY_POOL_ID, MainApplication.AWS_REGION)
AWSMobileClient.getInstance().initialize(activity, object : AWSStartupHandler {
override fun onComplete(awsStartupResult: AWSStartupResult) {
// Add code to instantiate a AmazonDynamoDBClient
val dynamoDBClient = AmazonDynamoDBClient(AWSMobileClient.getInstance().credentialsProvider)
val dbMapper = DynamoDBMapper.builder()
.dynamoDBClient(dynamoDBClient)
.awsConfiguration(
AWSMobileClient.getInstance().configuration)
.build()
val item = ItemDO()
item.id = UUID.randomUUID().toString() + UUID.randomUUID().toString()
item.name = view?.item_tf_name?.text.toString()
item.description = view?.item_tf_description?.text.toString()
item.userId = cognitoCredentialsProvider.cachedIdentityId
dbMapper.save(item, DynamoDBMapperConfig(DynamoDBMapperConfig.SaveBehavior.UPDATE_SKIP_NULL_ATTRIBUTES))
}
}).execute()
}
}
但是,当我调用该函数时,它给了我这个错误。
Process: com.thegoodyard.waiyanhein.thegoodyard, PID: 18353
com.amazonaws.AmazonServiceException: One or more parameter values were invalid: Missing the key Id in the item (Service: AmazonDynamoDB; Status Code: 400; Error Code: ValidationException; Request ID: 6N9KPON73BG135594S7GBV11IFVV4KQNSO5AEMVJF66Q9ASUAAJG)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:730)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:405)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:212)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:4182)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.putItem(AmazonDynamoDBClient.java:1370)
at com.amazonaws.mobileconnectors.dynamodbv2.document.Table.putItem(Table.java:280)
at com.amazonaws.mobileconnectors.dynamodbv2.document.Table.putItem(Table.java:261)
at com.thegoodyard.waiyanhein.thegoodyard.ItemFormFragment$createItem$1.invoke(ItemFormFragment.kt:75)
at com.thegoodyard.waiyanhein.thegoodyard.ItemFormFragment$createItem$1.invoke(ItemFormFragment.kt:16)
at kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)
但是我具有在类定义中映射ID的属性。我的代码有什么问题,我该如何解决?