├── .gitignore ├── README.md ├── pom.xml └── src ├── main ├── java │ └── guru │ │ └── springframework │ │ ├── SpringBootRedisApplication.java │ │ ├── commands │ │ └── ProductForm.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 ├── SpringBootMySqlApplicationTests.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-redis-example -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | guru.springframework 7 | spring-boot-redis 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-boot-redis 12 | Demo project for Spring Boot and Redis 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-redis 31 | 1.4.4.RELEASE 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-thymeleaf 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-web 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | 60 | spring-snapshots 61 | Spring Snapshots 62 | https://repo.spring.io/snapshot 63 | 64 | true 65 | 66 | 67 | 68 | spring-milestones 69 | Spring Milestones 70 | https://repo.spring.io/milestone 71 | 72 | false 73 | 74 | 75 | 76 | 77 | 78 | 79 | spring-snapshots 80 | Spring Snapshots 81 | https://repo.spring.io/snapshot 82 | 83 | true 84 | 85 | 86 | 87 | spring-milestones 88 | Spring Milestones 89 | https://repo.spring.io/milestone 90 | 91 | false 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/SpringBootRedisApplication.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 SpringBootRedisApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootRedisApplication.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 | 6 | /** 7 | * Created by jt on 1/10/17. 8 | */ 9 | public class ProductForm { 10 | private String id; 11 | private String description; 12 | private BigDecimal price; 13 | private String imageUrl; 14 | 15 | public String getId() { 16 | return id; 17 | } 18 | 19 | public void setId(String id) { 20 | this.id = id; 21 | } 22 | 23 | public String getDescription() { 24 | return description; 25 | } 26 | 27 | public void setDescription(String description) { 28 | this.description = description; 29 | } 30 | 31 | public BigDecimal getPrice() { 32 | return price; 33 | } 34 | 35 | public void setPrice(BigDecimal price) { 36 | this.price = price; 37 | } 38 | 39 | public String getImageUrl() { 40 | return imageUrl; 41 | } 42 | 43 | public void setImageUrl(String imageUrl) { 44 | this.imageUrl = imageUrl; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /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 | 17 | /** 18 | * Created by jt on 1/10/17. 19 | */ 20 | @Controller 21 | public class ProductController { 22 | private ProductService productService; 23 | 24 | private ProductToProductForm productToProductForm; 25 | 26 | @Autowired 27 | public void setProductToProductForm(ProductToProductForm productToProductForm) { 28 | this.productToProductForm = productToProductForm; 29 | } 30 | 31 | @Autowired 32 | public void setProductService(ProductService productService) { 33 | this.productService = productService; 34 | } 35 | 36 | @RequestMapping("/") 37 | public String redirToList(){ 38 | return "redirect:/product/list"; 39 | } 40 | 41 | @RequestMapping({"/product/list", "/product"}) 42 | public String listProducts(Model model){ 43 | model.addAttribute("products", productService.listAll()); 44 | return "product/list"; 45 | } 46 | 47 | @RequestMapping("/product/show/{id}") 48 | public String getProduct(@PathVariable String id, Model model){ 49 | model.addAttribute("product", productService.getById(id)); 50 | return "product/show"; 51 | } 52 | 53 | @RequestMapping("product/edit/{id}") 54 | public String edit(@PathVariable String id, Model model){ 55 | Product product = productService.getById(id); 56 | ProductForm productForm = productToProductForm.convert(product); 57 | 58 | model.addAttribute("productForm", productForm); 59 | return "product/productform"; 60 | } 61 | 62 | @RequestMapping("/product/new") 63 | public String newProduct(Model model){ 64 | model.addAttribute("productForm", new ProductForm()); 65 | return "product/productform"; 66 | } 67 | 68 | @RequestMapping(value = "/product", method = RequestMethod.POST) 69 | public String saveOrUpdateProduct(@Valid ProductForm productForm, BindingResult bindingResult){ 70 | 71 | if(bindingResult.hasErrors()){ 72 | return "product/productform"; 73 | } 74 | 75 | Product savedProduct = productService.saveOrUpdateProductForm(productForm); 76 | 77 | return "redirect:/product/show/" + savedProduct.getId(); 78 | } 79 | 80 | @RequestMapping("/product/delete/{id}") 81 | public String delete(@PathVariable String id){ 82 | productService.delete(id); 83 | return "redirect:/product/list"; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /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 org.springframework.data.annotation.Id; 4 | import org.springframework.data.redis.core.RedisHash; 5 | 6 | import java.math.BigDecimal; 7 | 8 | /** 9 | * Created by jt on 1/10/17. 10 | */ 11 | @RedisHash("products") 12 | public class Product { 13 | @Id 14 | private String id; 15 | private String description; 16 | private BigDecimal price; 17 | private String imageUrl; 18 | 19 | public String getId() { 20 | return id; 21 | } 22 | 23 | public void setId(String id) { 24 | this.id = id; 25 | } 26 | 27 | public String getDescription() { 28 | return description; 29 | } 30 | 31 | public void setDescription(String description) { 32 | this.description = description; 33 | } 34 | 35 | public BigDecimal getPrice() { 36 | return price; 37 | } 38 | 39 | public void setPrice(BigDecimal price) { 40 | this.price = price; 41 | } 42 | 43 | public String getImageUrl() { 44 | return imageUrl; 45 | } 46 | 47 | public void setImageUrl(String imageUrl) { 48 | this.imageUrl = imageUrl; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /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 | /** 7 | * Created by jt on 1/10/17. 8 | */ 9 | public interface ProductRepository extends CrudRepository { 10 | } 11 | -------------------------------------------------------------------------------- /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 | 8 | /** 9 | * Created by jt on 1/10/17. 10 | */ 11 | public interface ProductService { 12 | 13 | List listAll(); 14 | 15 | Product getById(String id); 16 | 17 | Product saveOrUpdate(Product product); 18 | 19 | void delete(String id); 20 | 21 | Product saveOrUpdateProductForm(ProductForm productForm); 22 | } 23 | -------------------------------------------------------------------------------- /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 | 13 | /** 14 | * Created by jt on 1/10/17. 15 | */ 16 | @Service 17 | public class ProductServiceImpl implements ProductService { 18 | 19 | private ProductRepository productRepository; 20 | private ProductFormToProduct productFormToProduct; 21 | 22 | @Autowired 23 | public ProductServiceImpl(ProductRepository productRepository, ProductFormToProduct productFormToProduct) { 24 | this.productRepository = productRepository; 25 | this.productFormToProduct = productFormToProduct; 26 | } 27 | 28 | 29 | @Override 30 | public List listAll() { 31 | List products = new ArrayList<>(); 32 | productRepository.findAll().forEach(products::add); //fun with Java 8 33 | return products; 34 | } 35 | 36 | @Override 37 | public Product getById(String id) { 38 | return productRepository.findById(id).orElse(null); 39 | } 40 | 41 | @Override 42 | public Product saveOrUpdate(Product product) { 43 | productRepository.save(product); 44 | return product; 45 | } 46 | 47 | @Override 48 | public void delete(String id) { 49 | productRepository.deleteById(id); 50 | 51 | } 52 | 53 | @Override 54 | public Product saveOrUpdateProductForm(ProductForm productForm) { 55 | Product savedProduct = saveOrUpdate(productFormToProduct.convert(productForm)); 56 | 57 | System.out.println("Saved Product Id: " + savedProduct.getId()); 58 | return savedProduct; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/springframeworkguru/spring-boot-redis-example/f5275bd0dbc998154c16dd223922b0254d0ac695/src/main/resources/application.properties -------------------------------------------------------------------------------- /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/SpringBootMySqlApplicationTests.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 SpringBootMySqlApplicationTests { 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 | } --------------------------------------------------------------------------------