BAEL-4025:Spring Data Azure Cosmos DB
This commit is contained in:
parent
0c032b90bb
commit
c95dbf8135
|
@ -0,0 +1,48 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-data-cosmosdb</artifactId>
|
||||
<name>spring-data-cosmos-db</name>
|
||||
<description>tutorial for spring-data-cosmosdb</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>spring-data-cosmosdb</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.spring.data.cosmosdb;
|
||||
|
||||
import com.azure.data.cosmos.CosmosKeyCredential;
|
||||
import com.baeldung.spring.data.cosmosdb.repository.ProductRepository;
|
||||
import com.microsoft.azure.spring.data.cosmosdb.config.AbstractCosmosConfiguration;
|
||||
import com.microsoft.azure.spring.data.cosmosdb.config.CosmosDBConfig;
|
||||
import com.microsoft.azure.spring.data.cosmosdb.repository.config.EnableCosmosRepositories;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableCosmosRepositories(basePackageClasses = ProductRepository.class)
|
||||
public class AzurecosmodbApplication extends AbstractCosmosConfiguration {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AzurecosmodbApplication.class, args);
|
||||
}
|
||||
|
||||
@Value("${azure.cosmosdb.uri}")
|
||||
private String uri;
|
||||
|
||||
@Value("${azure.cosmosdb.key}")
|
||||
private String key;
|
||||
|
||||
@Value("${azure.cosmosdb.secondaryKey}")
|
||||
private String secondaryKey;
|
||||
|
||||
@Value("${azure.cosmosdb.database}")
|
||||
private String dbName;
|
||||
|
||||
private CosmosKeyCredential cosmosKeyCredential;
|
||||
|
||||
@Bean
|
||||
public CosmosDBConfig getConfig() {
|
||||
this.cosmosKeyCredential = new CosmosKeyCredential(key);
|
||||
CosmosDBConfig cosmosdbConfig = CosmosDBConfig.builder(uri, this.cosmosKeyCredential, dbName)
|
||||
.build();
|
||||
return cosmosdbConfig;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.spring.data.cosmosdb.controller;
|
||||
|
||||
import com.baeldung.spring.data.cosmosdb.entity.Product;
|
||||
import com.baeldung.spring.data.cosmosdb.service.ProductService;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/products")
|
||||
public class ProductController {
|
||||
|
||||
@Autowired
|
||||
private ProductService productService;
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create(@RequestBody Product product) {
|
||||
productService.saveProduct(product);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{id}/category/{category}")
|
||||
public Optional<Product> get(@PathVariable String id, @PathVariable String category) {
|
||||
return productService.findById(id, category);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/{id}/category/{category}")
|
||||
public void delete(@PathVariable String id, @PathVariable String category) {
|
||||
productService.delete(id, category);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Product> getByName(@RequestParam String name) {
|
||||
return productService.findProductByName(name);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/category/{category}")
|
||||
public List<Product> getByCategory(@PathVariable String category) {
|
||||
return productService.getProductsOfCategory(category);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.spring.data.cosmosdb.entity;
|
||||
|
||||
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.Document;
|
||||
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.PartitionKey;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
@Document(collection = "products")
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
private String productid;
|
||||
|
||||
private String productName;
|
||||
|
||||
private double price;
|
||||
|
||||
@PartitionKey
|
||||
private String productCategory;
|
||||
|
||||
public String getProductid() {
|
||||
return productid;
|
||||
}
|
||||
|
||||
public void setProductid(String productid) {
|
||||
this.productid = productid;
|
||||
}
|
||||
|
||||
public String getProductName() {
|
||||
return productName;
|
||||
}
|
||||
|
||||
public void setProductName(String productName) {
|
||||
this.productName = productName;
|
||||
}
|
||||
|
||||
public String getProductCategory() {
|
||||
return productCategory;
|
||||
}
|
||||
|
||||
public void setProductCategory(String productCategory) {
|
||||
this.productCategory = productCategory;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Product [productid=" + productid + ", productName=" + productName + ", price=" + price + ", productCategory=" + productCategory + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.spring.data.cosmosdb.repository;
|
||||
|
||||
import com.baeldung.spring.data.cosmosdb.entity.Product;
|
||||
import com.microsoft.azure.spring.data.cosmosdb.repository.CosmosRepository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ProductRepository extends CosmosRepository<Product, String> {
|
||||
List<Product> findByProductName(String productName);
|
||||
|
||||
List<Product> findByProductCategory(String category);
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.spring.data.cosmosdb.service;
|
||||
|
||||
import com.azure.data.cosmos.PartitionKey;
|
||||
import com.baeldung.spring.data.cosmosdb.entity.Product;
|
||||
import com.baeldung.spring.data.cosmosdb.repository.ProductRepository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class ProductService {
|
||||
|
||||
@Autowired
|
||||
private ProductRepository repository;
|
||||
|
||||
public List<Product> findProductByName(String productName) {
|
||||
return repository.findByProductName(productName);
|
||||
}
|
||||
|
||||
public Optional<Product> findById(String productId, String category) {
|
||||
return repository.findById(productId, new PartitionKey(category));
|
||||
}
|
||||
|
||||
public List<Product> getProductsOfCategory(String category) {
|
||||
List<Product> products = repository.findByProductCategory(category);
|
||||
return products;
|
||||
}
|
||||
|
||||
public void saveProduct(Product product) {
|
||||
repository.save(product);
|
||||
}
|
||||
|
||||
public void delete(String productId, String category) {
|
||||
repository.deleteById(productId, new PartitionKey(category));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
azure.cosmosdb.uri=cosmodb-uri
|
||||
azure.cosmosdb.key=cosmodb-primary-key
|
||||
azure.cosmosdb.secondaryKey=cosmodb-second-key
|
||||
azure.cosmosdb.database=cosmodb-name
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.spring.data.cosmosdb;
|
||||
|
||||
import com.azure.data.cosmos.PartitionKey;
|
||||
import com.baeldung.spring.data.cosmosdb.entity.Product;
|
||||
import com.baeldung.spring.data.cosmosdb.repository.ProductRepository;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
//Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties
|
||||
//to run the integration test
|
||||
//@SpringBootTest
|
||||
public class AzurecosmodbApplicationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ProductRepository productRepository;
|
||||
|
||||
// Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties
|
||||
// to run the integration test
|
||||
// @Test
|
||||
public void givenProductIsCreated_whenCallFindById_thenProductIsFound() {
|
||||
Product product = new Product();
|
||||
product.setProductid("1001");
|
||||
product.setProductCategory("Shirt");
|
||||
product.setPrice(110.0);
|
||||
product.setProductName("Blue Shirt");
|
||||
|
||||
// Uncomment these lines when configured URI and keys for Azure Cosmos DB in application.properties
|
||||
// to run the integration test
|
||||
// productRepository.save(product);
|
||||
// Optional<Product> retrievedProduct = productRepository.findById("1001", new PartitionKey("Shirt"));
|
||||
// Assert.notNull(retrievedProduct, "Retrieved Product is Null");
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue