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.
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.
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>spring-data-cosmosdb</artifactId> <version>2.0.3</version> </dependency>
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<Role> 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<User, String> { List<User> findByName(String firstName); List<User> 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<User, String> { List<User> findByName(String firstName); ... }
Next steps
For more information about using Spring on Azure, visit the following pages:
- GitHub: Spring Data Azure Cosmos DB
- GitHub: Spring Boot Starters for Azure Services
- Tutorial: Spring on Azure developer center
- Tutorial: Java on Azure developer center
Feedback
Please share your feedback and ask questions to help us improve. You can contact us on Gitter.