├── .gitignore ├── README.md ├── pom.xml └── src ├── main ├── java │ └── guru │ │ └── springframework │ │ ├── SpringBootCassandraApplication.java │ │ ├── commands │ │ └── ProductForm.java │ │ ├── config │ │ └── CassandraConfig.java │ │ ├── controllers │ │ └── ProductController.java │ │ ├── converters │ │ ├── ProductFormToProduct.java │ │ └── ProductToProductForm.java │ │ ├── domain │ │ └── Product.java │ │ ├── repositories │ │ └── ProductRepository.java │ │ └── services │ │ ├── ProductService.java │ │ └── ProductServiceImpl.java └── resources │ ├── application.properties │ └── templates │ └── product │ ├── list.html │ ├── productform.html │ └── show.html └── test └── java └── guru └── springframework ├── SpringBootCassandraApplicationTests.java └── repositories └── ProductRepositoryTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | 14 | target 15 | 16 | # IDE files 17 | .idea 18 | *.iml 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-cassandra-example -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | guru.springframework 7 | spring-boot-cassandra 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-boot-cassandra 12 | Demo project for Spring Boot and cassandra 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-cassandra 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-thymeleaf 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-web 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | 58 | spring-snapshots 59 | Spring Snapshots 60 | https://repo.spring.io/snapshot 61 | 62 | true 63 | 64 | 65 | 66 | spring-milestones 67 | Spring Milestones 68 | https://repo.spring.io/milestone 69 | 70 | false 71 | 72 | 73 | 74 | 75 | 76 | 77 | spring-snapshots 78 | Spring Snapshots 79 | https://repo.spring.io/snapshot 80 | 81 | true 82 | 83 | 84 | 85 | spring-milestones 86 | Spring Milestones 87 | https://repo.spring.io/milestone 88 | 89 | false 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/SpringBootCassandraApplication.java: -------------------------------------------------------------------------------- 1 | package guru.springframework; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootCassandraApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootCassandraApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/commands/ProductForm.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.commands; 2 | 3 | 4 | import java.math.BigDecimal; 5 | import java.util.UUID; 6 | 7 | /** 8 | * Created by jt on 1/10/17. 9 | */ 10 | public class ProductForm { 11 | private UUID id; 12 | private String description; 13 | private BigDecimal price; 14 | private String imageUrl; 15 | 16 | public UUID getId() { 17 | return id; 18 | } 19 | 20 | public void setId(UUID id) { 21 | this.id = id; 22 | } 23 | 24 | public String getDescription() { 25 | return description; 26 | } 27 | 28 | public void setDescription(String description) { 29 | this.description = description; 30 | } 31 | 32 | public BigDecimal getPrice() { 33 | return price; 34 | } 35 | 36 | public void setPrice(BigDecimal price) { 37 | this.price = price; 38 | } 39 | 40 | public String getImageUrl() { 41 | return imageUrl; 42 | } 43 | 44 | public void setImageUrl(String imageUrl) { 45 | this.imageUrl = imageUrl; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/config/CassandraConfig.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.config; 2 | 3 | 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.cassandra.config.AbstractCassandraConfiguration; 6 | import org.springframework.data.cassandra.config.SchemaAction; 7 | import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification; 8 | import org.springframework.data.cassandra.core.cql.keyspace.DropKeyspaceSpecification; 9 | 10 | import java.util.Arrays; 11 | import java.util.List; 12 | 13 | /** 14 | * Created by jt on 10/6/17. 15 | */ 16 | @Configuration 17 | public class CassandraConfig extends AbstractCassandraConfiguration { 18 | 19 | public static final String KEYSPACE = "guru_keyspace"; 20 | 21 | @Override 22 | public SchemaAction getSchemaAction() { 23 | return SchemaAction.CREATE_IF_NOT_EXISTS; 24 | } 25 | 26 | @Override 27 | protected List getKeyspaceCreations() { 28 | CreateKeyspaceSpecification specification = CreateKeyspaceSpecification.createKeyspace(KEYSPACE); 29 | 30 | return Arrays.asList(specification); 31 | } 32 | 33 | @Override 34 | protected List getKeyspaceDrops() { 35 | return Arrays.asList(DropKeyspaceSpecification.dropKeyspace(KEYSPACE)); 36 | } 37 | 38 | @Override 39 | protected String getKeyspaceName() { 40 | return KEYSPACE; 41 | } 42 | 43 | @Override 44 | public String[] getEntityBasePackages() { 45 | return new String[]{"guru.springframework.domain"}; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/controllers/ProductController.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.controllers; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.converters.ProductToProductForm; 5 | import guru.springframework.domain.Product; 6 | import guru.springframework.services.ProductService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Controller; 9 | import org.springframework.ui.Model; 10 | import org.springframework.validation.BindingResult; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | 15 | import javax.validation.Valid; 16 | import java.util.UUID; 17 | 18 | /** 19 | * Created by jt on 1/10/17. 20 | */ 21 | @Controller 22 | public class ProductController { 23 | private ProductService productService; 24 | 25 | private ProductToProductForm productToProductForm; 26 | 27 | @Autowired 28 | public void setProductToProductForm(ProductToProductForm productToProductForm) { 29 | this.productToProductForm = productToProductForm; 30 | } 31 | 32 | @Autowired 33 | public void setProductService(ProductService productService) { 34 | this.productService = productService; 35 | } 36 | 37 | @RequestMapping("/") 38 | public String redirToList(){ 39 | return "redirect:/product/list"; 40 | } 41 | 42 | @RequestMapping({"/product/list", "/product"}) 43 | public String listProducts(Model model){ 44 | model.addAttribute("products", productService.listAll()); 45 | return "product/list"; 46 | } 47 | 48 | @RequestMapping("/product/show/{id}") 49 | public String getProduct(@PathVariable String id, Model model){ 50 | model.addAttribute("product", productService.getById(UUID.fromString(id))); 51 | return "product/show"; 52 | } 53 | 54 | @RequestMapping("product/edit/{id}") 55 | public String edit(@PathVariable String id, Model model){ 56 | Product product = productService.getById(UUID.fromString(id)); 57 | ProductForm productForm = productToProductForm.convert(product); 58 | 59 | model.addAttribute("productForm", productForm); 60 | return "product/productform"; 61 | } 62 | 63 | @RequestMapping("/product/new") 64 | public String newProduct(Model model){ 65 | model.addAttribute("productForm", new ProductForm()); 66 | return "product/productform"; 67 | } 68 | 69 | @RequestMapping(value = "/product", method = RequestMethod.POST) 70 | public String saveOrUpdateProduct(@Valid ProductForm productForm, BindingResult bindingResult){ 71 | 72 | if(bindingResult.hasErrors()){ 73 | return "product/productform"; 74 | } 75 | 76 | Product savedProduct = productService.saveOrUpdateProductForm(productForm); 77 | 78 | return "redirect:/product/show/" + savedProduct.getId(); 79 | } 80 | 81 | @RequestMapping("/product/delete/{id}") 82 | public String delete(@PathVariable String id){ 83 | productService.delete(UUID.fromString(id)); 84 | return "redirect:/product/list"; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/converters/ProductFormToProduct.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.converters; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.domain.Product; 5 | import org.springframework.core.convert.converter.Converter; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.util.StringUtils; 8 | 9 | /** 10 | * Created by jt on 1/10/17. 11 | */ 12 | @Component 13 | public class ProductFormToProduct implements Converter { 14 | 15 | @Override 16 | public Product convert(ProductForm productForm) { 17 | Product product = new Product(); 18 | if (productForm.getId() != null && !StringUtils.isEmpty(productForm.getId())) { 19 | product.setId(productForm.getId()); 20 | } 21 | product.setDescription(productForm.getDescription()); 22 | product.setPrice(productForm.getPrice()); 23 | product.setImageUrl(productForm.getImageUrl()); 24 | return product; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/converters/ProductToProductForm.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.converters; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.domain.Product; 5 | import org.springframework.core.convert.converter.Converter; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * Created by jt on 1/10/17. 10 | */ 11 | @Component 12 | public class ProductToProductForm implements Converter { 13 | @Override 14 | public ProductForm convert(Product product) { 15 | ProductForm productForm = new ProductForm(); 16 | productForm.setId(product.getId()); 17 | productForm.setDescription(product.getDescription()); 18 | productForm.setPrice(product.getPrice()); 19 | productForm.setImageUrl(product.getImageUrl()); 20 | return productForm; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/domain/Product.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.domain; 2 | 3 | import com.datastax.driver.core.DataType; 4 | import org.springframework.data.cassandra.core.mapping.CassandraType; 5 | import org.springframework.data.cassandra.core.mapping.PrimaryKey; 6 | import org.springframework.data.cassandra.core.mapping.Table; 7 | 8 | 9 | import java.io.Serializable; 10 | import java.math.BigDecimal; 11 | import java.util.UUID; 12 | 13 | /** 14 | * Created by jt on 1/10/17. 15 | */ 16 | @Table("products") 17 | public class Product implements Serializable{ 18 | 19 | @PrimaryKey 20 | @CassandraType(type = DataType.Name.UUID) 21 | private UUID id; 22 | private String description; 23 | private BigDecimal price; 24 | private String imageUrl; 25 | 26 | public UUID getId() { 27 | return id; 28 | } 29 | 30 | public void setId(UUID id) { 31 | this.id = id; 32 | } 33 | 34 | public String getDescription() { 35 | return description; 36 | } 37 | 38 | public void setDescription(String description) { 39 | this.description = description; 40 | } 41 | 42 | public BigDecimal getPrice() { 43 | return price; 44 | } 45 | 46 | public void setPrice(BigDecimal price) { 47 | this.price = price; 48 | } 49 | 50 | public String getImageUrl() { 51 | return imageUrl; 52 | } 53 | 54 | public void setImageUrl(String imageUrl) { 55 | this.imageUrl = imageUrl; 56 | } 57 | 58 | public Product() { 59 | id = UUID.randomUUID(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/repositories/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.repositories; 2 | 3 | import guru.springframework.domain.Product; 4 | import org.springframework.data.repository.CrudRepository; 5 | 6 | import java.util.UUID; 7 | 8 | /** 9 | * Created by jt on 1/10/17. 10 | */ 11 | public interface ProductRepository extends CrudRepository { 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/services/ProductService.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.services; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.domain.Product; 5 | 6 | import java.util.List; 7 | import java.util.UUID; 8 | 9 | /** 10 | * Created by jt on 1/10/17. 11 | */ 12 | public interface ProductService { 13 | 14 | List listAll(); 15 | 16 | Product getById(UUID id); 17 | 18 | Product saveOrUpdate(Product product); 19 | 20 | void delete(UUID id); 21 | 22 | Product saveOrUpdateProductForm(ProductForm productForm); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/services/ProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.services; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.converters.ProductFormToProduct; 5 | import guru.springframework.domain.Product; 6 | import guru.springframework.repositories.ProductRepository; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | import java.util.UUID; 13 | 14 | /** 15 | * Created by jt on 1/10/17. 16 | */ 17 | @Service 18 | public class ProductServiceImpl implements ProductService { 19 | 20 | private ProductRepository productRepository; 21 | private ProductFormToProduct productFormToProduct; 22 | 23 | @Autowired 24 | public ProductServiceImpl(ProductRepository productRepository, ProductFormToProduct productFormToProduct) { 25 | this.productRepository = productRepository; 26 | this.productFormToProduct = productFormToProduct; 27 | } 28 | 29 | 30 | @Override 31 | public List listAll() { 32 | List products = new ArrayList<>(); 33 | productRepository.findAll().forEach(products::add); //fun with Java 8 34 | return products; 35 | } 36 | 37 | @Override 38 | public Product getById(UUID id) { 39 | return productRepository.findById(id).orElse(null); 40 | } 41 | 42 | @Override 43 | public Product saveOrUpdate(Product product) { 44 | productRepository.save(product); 45 | return product; 46 | } 47 | 48 | @Override 49 | public void delete(UUID id) { 50 | productRepository.deleteById(id); 51 | 52 | } 53 | 54 | @Override 55 | public Product saveOrUpdateProductForm(ProductForm productForm) { 56 | Product savedProduct = saveOrUpdate(productFormToProduct.convert(productForm)); 57 | 58 | System.out.println("Saved Product Id: " + savedProduct.getId()); 59 | return savedProduct; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # =============================== 2 | # = DATA SOURCE 3 | # =============================== 4 | # Set here configurations for the database connection 5 | spring.data.cassandra.keyspace-name=guru_keyspace 6 | spring.data.cassandra.contact-points=localhost 7 | spring.data.cassandra.port=9042 8 | spring.data.cassandra.schema-action=create_if_not_exists 9 | 10 | -------------------------------------------------------------------------------- /src/main/resources/templates/product/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Core Online Tutorial - List Products 5 | 6 | 7 | 10 | 11 | 13 | 14 | 16 | 17 | 18 |
19 |
20 |

Product List

21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
IdDescriptionPriceImage URLListEditDelete
View Edit Delete
41 |
42 |
43 |
44 | New Product 45 |
46 |
47 |
48 | 49 | 50 | -------------------------------------------------------------------------------- /src/main/resources/templates/product/productform.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Core Online Tutorial - Product Form 5 | 6 | 7 | 10 | 11 | 13 | 14 | 16 | 17 | 18 |
19 | 20 |

Product Details

21 |
22 |
23 | 24 |
25 |

Error Message

26 |
27 | 28 | 29 | 30 |
31 | 32 |
33 | 34 | 35 | 36 |
    37 |
  • 38 |
39 |
40 |
41 |
42 | 43 |
44 | 45 |
46 | 47 | 48 | 49 |
    50 |
  • 51 |
52 |
53 |
54 |
55 | 56 |
57 | 58 |
59 | 60 | 61 | 62 |
    63 |
  • 64 |
65 |
66 |
67 |
68 |
69 | 70 |
71 |
72 |
73 |
74 | 75 | 76 | -------------------------------------------------------------------------------- /src/main/resources/templates/product/show.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Core Online Tutorial - Show Product 5 | 6 | 7 | 10 | 11 | 13 | 14 | 16 | 17 | 18 |
19 | 20 |
21 |
22 |

Show Product

23 |
24 |
25 |
26 |
27 |
28 |
29 | 30 |
31 |

Product Id

32 |
33 |
34 |
35 | 36 |
37 |

Description

38 |
39 |
40 |
41 | 42 |
43 |

Price

44 |
45 |
46 |
47 | 48 |
49 |

Image

50 |
51 |
52 |
53 |
54 |
55 |
56 | 57 | 58 | -------------------------------------------------------------------------------- /src/test/java/guru/springframework/SpringBootCassandraApplicationTests.java: -------------------------------------------------------------------------------- 1 | package guru.springframework; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootCassandraApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/guru/springframework/repositories/ProductRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.repositories; 2 | 3 | import guru.springframework.domain.Product; 4 | import org.junit.Assert; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import java.math.BigDecimal; 13 | 14 | @RunWith(SpringJUnit4ClassRunner.class) 15 | @SpringBootTest 16 | public class ProductRepositoryTest { 17 | 18 | private static final BigDecimal BIG_DECIMAL_100 = BigDecimal.valueOf(100.00); 19 | private static final String PRODUCT_DESCRIPTION = "a cool product"; 20 | private static final String IMAGE_URL = "http://an-imageurl.com/image1.jpg"; 21 | 22 | @Autowired 23 | private ProductRepository productRepository; 24 | 25 | @Before 26 | public void setUp() throws Exception { 27 | 28 | } 29 | 30 | @Test 31 | public void testPersistence() { 32 | //given 33 | Product product = new Product(); 34 | product.setDescription(PRODUCT_DESCRIPTION); 35 | product.setImageUrl(IMAGE_URL); 36 | product.setPrice(BIG_DECIMAL_100); 37 | 38 | //when 39 | productRepository.save(product); 40 | 41 | //then 42 | Assert.assertNotNull(product.getId()); 43 | Product newProduct = productRepository.findById(product.getId()).orElse(null); 44 | Assert.assertEquals(PRODUCT_DESCRIPTION, newProduct.getDescription()); 45 | Assert.assertEquals(BIG_DECIMAL_100.compareTo(newProduct.getPrice()), 0); 46 | Assert.assertEquals(IMAGE_URL, newProduct.getImageUrl()); 47 | } 48 | } --------------------------------------------------------------------------------