增加 spring cloud 用例
This commit is contained in:
@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<groupId>com.heibaiying.ribbon</groupId>
|
||||
<artifactId>spring-cloud-ribbon</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
@ -31,7 +31,7 @@
|
||||
</dependency>
|
||||
<!--引入对公共模块的依赖-->
|
||||
<dependency>
|
||||
<groupId>com.heibaiying.eureka</groupId>
|
||||
<groupId>com.heibaiying.ribbon</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
|
@ -6,9 +6,9 @@ import com.heibaiying.common.bean.Product;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -21,17 +21,25 @@ public class SellController {
|
||||
@Autowired
|
||||
private IProductService productService;
|
||||
|
||||
@RequestMapping
|
||||
@GetMapping("products")
|
||||
public String productList(Model model) {
|
||||
List<Product> products = productService.queryAllProducts();
|
||||
model.addAttribute("products", products);
|
||||
return "products";
|
||||
}
|
||||
|
||||
@RequestMapping("product/{id}")
|
||||
@GetMapping("product/{id}")
|
||||
public String productDetail(@PathVariable int id, Model model) {
|
||||
Product product = productService.queryProductById(id);
|
||||
model.addAttribute("product", product);
|
||||
return "product";
|
||||
}
|
||||
|
||||
@PostMapping("product")
|
||||
public String save(@RequestParam String productName) {
|
||||
long id = Math.round(Math.random() * 100);
|
||||
Product product = new Product(id, productName, false, new Date(), 88);
|
||||
productService.saveProduct(product);
|
||||
return "redirect:products";
|
||||
}
|
||||
}
|
||||
|
@ -31,4 +31,7 @@ public class ProductService implements IProductService {
|
||||
return productList;
|
||||
}
|
||||
|
||||
public void saveProduct(Product product) {
|
||||
restTemplate.postForObject("http://producer/product", product, Void.class);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 8030
|
||||
port: 8080
|
||||
# 指定服务命名
|
||||
spring:
|
||||
application:
|
||||
|
@ -5,11 +5,15 @@
|
||||
</head>
|
||||
<body>
|
||||
<h3>产品列表:点击查看详情</h3>
|
||||
<form action="/sell/product" method="post">
|
||||
<input type="text" name="productName">
|
||||
<input type="submit" value="新增产品">
|
||||
</form>
|
||||
<ul>
|
||||
<#list products as product>
|
||||
<li>
|
||||
<a href="sell/product/${product.id}">${product.name}</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="/sell/product/${product.id}">${product.name}</a>
|
||||
</li>
|
||||
</#list>
|
||||
</ul>
|
||||
</body>
|
||||
|
Reference in New Issue
Block a user