Java-DocumentDB未经授权的访问
我正在尝试编写一个功能,将Azure中的对象存储在DocumentDB中 我有一段代码:Java-DocumentDB未经授权的访问,java,azure,azure-cosmosdb,Java,Azure,Azure Cosmosdb,我正在尝试编写一个功能,将Azure中的对象存储在DocumentDB中 我有一段代码: public void saveEvent(Event event) throws DocumentClientException { Document document = new Document(JsonCreator.createJson(event)); //check if document already exists FeedOptions
public void saveEvent(Event event) throws DocumentClientException {
Document document = new Document(JsonCreator.createJson(event));
//check if document already exists
FeedOptions feedOptions = new FeedOptions();
feedOptions.setEnableCrossPartitionQuery(true);
FeedResponse<Document> eventDocument = documentClient.queryDocuments(COLLECTION_LINK, String.format(SELECT_DOCUMENT, event.getId()), feedOptions);
// if there is a document with the ID then replace
if (eventDocument.getQueryIterator().hasNext()) {
//documentClient.replaceDocument(COLLECTION_LINK, document, null);
documentClient.replaceDocument(COLLECTION_LINK, document, null);
}
else {
documentClient.createDocument(COLLECTION_LINK, document, null, false);
}
}
public void saveEvent(事件事件)抛出DocumentClientException{
Document Document=新文档(JsonCreator.createJson(事件));
//检查文档是否已经存在
FeedOptions FeedOptions=新的FeedOptions();
setEnableCrossPartitionQuery(true);
FeedResponse eventDocument=documentClient.queryDocuments(集合链接,String.format(选择文档,event.getId()),feedOptions);
//如果存在ID为的文档,则替换
if(eventDocument.getQueryIterator().hasNext()){
//documentClient.replaceDocument(集合链接,文档,空);
documentClient.replaceDocument(集合链接,文档,空);
}
否则{
documentClient.createDocument(集合链接,文档,null,false);
}
}
如果事件
不存在(意味着数据库中没有id为event
的id
的记录),则调用createDocument
。如果数据库中已存在该记录,则调用replaceDocument
- 调用
没有问题,文档在数据库中创建createDocument
抛出replaceDocument
StatusCode:Unauthorized exception
com.microsoft.azure.documentdb.DocumentClientException:输入授权令牌无法为请求提供服务。请检查预期有效负载是否按照协议构建,并检查正在使用的密钥。服务器使用以下有效负载进行签名:“put colls dbs/sporteventsdb/colls/sportevents sun,2017年3月26日08:32:41 gmt
完整堆栈:
代码中使用的常量:
COLLECTION\u LINK=“dbs/”+数据库ID+“/colls/”+集合ID”
SELECT\u DOCUMENT=“SELECT*FROM”+DATABASE\u ID+”其中“+DATABASE\u ID+”。ID=\%d\”
我正在用
Java8
开发Spring
framework onIntellijIDEA
onUbuntu
的Java8您会遇到错误,因为DocumentDB将documentLink
作为参数,而不是collectionLink。Javadoc在这里: