Skip to main content

 Subscribe

We are pleased to announce that Spring Data Azure Cosmos DB is now available to provide essential Spring Data support for Azure Cosmos DB using SQL API. Azure Cosmos DB is Microsoft’s globally distributed, multi-model database service with exceptional scalability and performance.

With Spring Data Azure Cosmos DB, Java developers now can get started quickly to build NoSQL data access for their apps on Azure. It offers a Spring-based programming model for data access, while keeping the special traits of the underlying data store with Azure Cosmos DB. Features of Spring Data Azure Cosmos DB include a POJO centric model for interacting with an Azure Cosmos DB Collection, and an extensible repository style data access layer.

Getting started

Download the Spring Data Azure Cosmos DB Sample Project to get started. The sample illustrates the process to use annotation to interact with Collection, customize a query operation with specific fields, and expose a discoverable REST API for clients.

Create a new database instance

To get started, first create a new database instance by using the Azure portal. You can find Azure Cosmos DB in Databases and choose SQL (Document DB) for the API. When your database has been created, you can find the URI and keys on the overview page. The values will be used to configure your Spring Boot application.

New database instance

Configure your project

You can create a simple Spring Boot application using Spring Initializr, and locate the pom.xml file in the directory of your app. In the pom.xml file add spring-data-cosmosdb to list of dependencies. spring-data-cosmosdb is published in Maven Central Repository. Please refer to this tutorial for detailed steps of configuration for database connection.  


    com.microsoft.azure
    spring-data-cosmosdb
    2.0.3

Features of Spring Data Azure Cosmos DB

Using Spring Data Azure Cosmos DB, you can get started quickly to build NoSQL data access for their apps on Azure.

Use Annotation to interact with Collection

@Id annotation: Annotate a field in domain class with @Id, this field will be mapped to document id in Azure Cosmos DB.

@Document annotation: By default, collection name will use the name of the domain class. To customize it, add annotation @Document(collection=”yourCollectionName”).

@Document(collection = "mycollection")
public class User {
    @Id
    private String id;
    private String email;
    private String name;
    private Address address;
    private List roleList;
   ...
}

Customize query operation

Customized query is useful for building constraining queries over entities of the repository. You can extend the basic DocumentDbRepository for different business logics.

public interface UserRepository extends DocumentDbRepository {

    List findByName(String firstName);
    List findByEmailAndAddress(String email, Address address);
    ...
}

Exposes a discoverable REST API

@RepositoryRestResource Annotation: expose a discoverable REST API for your domain model.

@Repository
@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends DocumentDbRepository {

    List findByName(String firstName);
    ...

}
 
For more advanced features, please visit our GitHub Repo.
 

Next steps

For more information about using Spring on Azure, visit the following pages:

Feedback

Please share your feedback and ask questions to help us improve. You can contact us on Gitter.

  • Explore

     

    Let us know what you think of Azure and what you would like to see in the future.

     

    Provide feedback

  • Build your cloud computing and Azure skills with free courses by Microsoft Learn.

     

    Explore Azure learning