1

I am trying to write a functionality to store an object in DocumentDB from Azure.

I have a following piece of code:

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

If the event does not exist (means that there is no record in database with the id of event ) then createDocument is called. If the record already exists in the database then replaceDocument is called.

  • createDocument is called without problem and document is created in the database
  • replaceDocument throws StatusCode: Unauthorized exception

com.microsoft.azure.documentdb.DocumentClientException: The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'put colls dbs/sporteventsdb/colls/sportevents sun, 26 mar 2017 08:32:41 gmt

Full stack: http://pastebin.com/YVGwqLkH

Constants used in the code:

  • COLLECTION_LINK = "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID";
  • SELECT_DOCUMENT = "SELECT * FROM " + DATABASE_ID + " WHERE " + DATABASE_ID + ".id = \"%d\"";

I'm developing with Spring framework on IntelliJ IDEA on Ubuntu with Java 8.

1 Answer 1

2

You're getting the error because DocumentDB's replaceDocument takes the documentLink as an argument, no the collectionLink. Javadoc is here: http://azure.github.io/azure-documentdb-java/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.