This sample project demonstrates how to use Azure CosmosDB via Spring Boot Starter azure-cosmosdb-spring-boot-starter
to store data in and retrieve data from your Azure Cosmos DB.
-
An Azure subscription; if you don't already have an Azure subscription, you can activate your MSDN subscriber benefits or sign up for a free Azure account.
-
A Java Development Kit (JDK), version 1.8.
-
Apache Maven, version 3.0 or later.
- Go to Azure portal and click +New .
- Click Databases, and then click Azure Cosmos DB to create your database.
- Navigate to the database you have created, and click Access keys and copy your URI and access keys for your database.
- Navigate to
src/main/resources
and openapplication.properties
. - replace below properties in
application.properties
with information of your database.azure.cosmosdb.uri=your-cosmosdb-uri azure.cosmosdb.key=your-cosmosdb-key azure.cosmosdb.database=your-cosmosdb-databasename
-
Change directory to folder
azure-cosmosdb-spring-boot-sample
. -
Run below commands.
-
Use Maven
mvn package java -jar target/azure-ducumentdb-spring-boot-sample-0.0.1-SNAPSHOT.jar
-
Use Gradle
gradle bootRepackage java -jar build/libs/azure-ducumentdb-spring-boot-sample-0.0.1-SNAPSHOT.jar
-
Directly running the sample app from IDE IntelliJ or Eclipse has below security exception if using the released starter. The root cause is that the release spring-data-azure-cosmosdb
jar is code-signed by us. We're working actively to resolve this issue.
Caused by: java.lang.SecurityException: class "com.microsoft.azure.sample.User_Accessor_yhb3bq"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(ClassLoader.java:898) ~[na:1.8.0_131]
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:668) ~[na:1.8.0_131]
at java.lang.ClassLoader.defineClass(ClassLoader.java:761) ~[na:1.8.0_131]
If com.fasterxml.jackson.databind.JsonMappingException
is thrown during deserialization, with error message Can not construct instance of {your.pojo.class}: no suitable constructor found, can not deserialize from Object value (missing default constructor...
, add Lombok annotatations @Data
and @AllArgsConstructor
for your POJO class, or use Jackson annotations @JsonCreator
and @JsonProperty
for the full argument constructor.
Please refer to this article for the tutorial about how to use the Spring Boot Starter with Azure Cosmos DB API.