Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-DocumentDB未经授权的访问_Java_Azure_Azure Cosmosdb - Fatal编程技术网

Java-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

我正在尝试编写一个功能,将Azure中的对象存储在DocumentDB

我有一段代码:

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 on
IntellijIDEA
on
Ubuntu
Java8

您会遇到错误,因为DocumentDB将
documentLink
作为参数,而不是collectionLink。Javadoc在这里: