Use Spring Data with Azure Cosmos DB for MongoDB API
This article demonstrates creating a sample application that uses Spring Data to store and retrieve information using Azure Cosmos DB for MongoDB.
Prerequisites
An Azure subscription - create one for free.
Java Development Kit (JDK), version 8 or higher.
- A Git client.
Create an Azure Cosmos DB account
Create an Azure Cosmos DB account using the Azure portal
Note
You can read more detailed information about creating accounts in the Azure Cosmos DB documentation.
Browse to the Azure portal at https://portal.azure.com/ and sign in.
Select Create a resource, then Databases, then Azure Cosmos DB.
On the Select API option screen, select Azure Cosmos DB for MongoDB.
Specify the following information:
- Subscription: Specify your Azure subscription to use.
- Resource group: Specify whether to create a new resource group, or choose an existing resource group.
- Account name: Choose a unique name for your Azure Cosmos DB account; this will be used to create a fully-qualified domain name like wingtiptoysmongodb.documents.azure.com.
- API: Specify
Azure Cosmos DB for MongoDB API
for this tutorial. - Location: Specify the closest geographic region for your database.
When you've entered all of the above information, click Review + create.
If everything looks correct on the review page, click Create.
Retrieve the connection string for your Azure Cosmos DB account
Browse to the Azure portal at https://portal.azure.com/ and sign in.
Click All Resources, then click the Azure Cosmos DB account you just created.
Click Connection strings, and copy the value for the Primary Connection String field; you'll use that value to configure your application later.
Configure the sample application
Open a command shell and clone the sample project using a git command like the following example:
git clone https://github.com/spring-guides/gs-accessing-data-mongodb.git
Create a resources directory in the <project root>/complete/src/main directory of the sample project, and create an application.properties file in the resources directory.
Open the application.properties file in a text editor, and add the following lines in the file, and replace the sample values with the appropriate values from earlier:
spring.data.mongodb.database=wingtiptoysmongodb spring.data.mongodb.uri=mongodb://wingtiptoysmongodb:AbCdEfGhIjKlMnOpQrStUvWxYz==@wingtiptoysmongodb.documents.azure.com:10255/?ssl=true&replicaSet=globaldb
Where:
Parameter Description spring.data.mongodb.database
Specifies the name of your Azure Cosmos DB account from earlier in this article. spring.data.mongodb.uri
Specifies the Primary Connection String from earlier in this article. Save and close the application.properties file.
Package and test the sample application
To build the application, browse to the directory /gs-accessing-data-mongodb/complete, which contains the pom.xml file.
Build the sample application with Maven, and configure Maven to skip tests; for example:
mvn clean package -DskipTests
Start the sample application; for example:
java -jar target/accessing-data-mongodb-complete-0.0.1-SNAPSHOT.jar
Your application should return values like the following:
Customers found with findAll(): ------------------------------- Customer[id=5c1b4ae4d0b5080ac105cc13, firstName='Alice', lastName='Smith'] Customer[id=5c1b4ae4d0b5080ac105cc14, firstName='Bob', lastName='Smith'] Customer found with findByFirstName('Alice'): -------------------------------- Customer[id=5c1b4ae4d0b5080ac105cc13, firstName='Alice', lastName='Smith'] Customers found with findByLastName('Smith'): -------------------------------- Customer[id=5c1b4ae4d0b5080ac105cc13, firstName='Alice', lastName='Smith'] Customer[id=5c1b4ae4d0b5080ac105cc14, firstName='Bob', lastName='Smith']
Summary
In this tutorial, you created a sample Java application that uses Spring Data to store and retrieve information using Azure Cosmos DB for MongoDB.
Clean up resources
When no longer needed, use the Azure portal to delete the resources created in this article to avoid unexpected charges.
Next steps
To learn more about Spring and Azure, continue to the Spring on Azure documentation center.
See also
For more information about using Azure with Java, see the Azure for Java Developers and the Working with Azure DevOps and Java.