商品微服务添加api接口
生活随笔
收集整理的這篇文章主要介紹了
商品微服务添加api接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
編寫跳轉controller
在learn-goods-web中編寫controller,接收請求,并跳轉到商品詳情頁:
@Controller @RequestMapping("item") public class GoodsController {/*** 跳轉到商品詳情頁* @param model* @param id* @return*/@GetMapping("{id}.html")public String toItemPage(Model model, @PathVariable("id")Long id){return "item";} }測試
啟動learn-goods-page,點擊搜索頁面商品,看是能夠正常跳轉:
現在看到的依然是靜態的數據。我們接下來開始頁面的渲染
?
封裝模型數據
首先我們一起來分析一下,在這個頁面中需要哪些數據
我們已知的條件是傳遞來的spu的id,我們需要根據spu的id查詢到下面的數據:
-
spu信息
-
spu的詳情
-
spu下的所有sku
-
品牌
-
商品三級分類
-
商品規格參數、規格參數組
商品微服務提供接口
1.4.1.1.查詢spu
以上所需數據中,根據id查詢spu的接口目前還沒有,我們需要在商品微服務中提供這個接口:
GoodsApi
/*** 根據spu的id查詢spu* @param id* @return*/ @GetMapping("spu/{id}") public Spu querySpuById(@PathVariable("id") Long id);GoodsController
@GetMapping("spu/{id}") public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){Spu spu = this.goodsService.querySpuById(id);if(spu == null){return new ResponseEntity<>(HttpStatus.NOT_FOUND);}return ResponseEntity.ok(spu); }GoodsService
public Spu querySpuById(Long id) {return this.spuMapper.selectByPrimaryKey(id); }查詢規格參數組
我們在頁面展示規格時,需要按組展示:
組內有多個參數,為了方便展示。我們在learn-item-service中提供一個接口,查詢規格組,同時在規格組內的所有參數。
拓展SpecGroup類:
我們在SpecGroup中添加一個SpecParam的集合,保存該組下所有規格參數
@Table(name = "tb_spec_group") public class SpecGroup {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private Long cid;private String name;@Transientprivate List<SpecParam> params; // 該組下的所有規格參數集合 }然后提供查詢接口:
SpecificationAPI:
@RequestMapping("spec") public interface SpecificationApi {@GetMapping("groups/{cid}")public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid);@GetMapping("/params")public List<SpecParam> querySpecParam(@RequestParam(value = "gid", required = false) Long gid,@RequestParam(value = "cid", required = false) Long cid,@RequestParam(value = "searching", required = false) Boolean searching,@RequestParam(value = "generic", required = false) Boolean generic);// 查詢規格參數組,及組內參數@GetMapping("{cid}")List<SpecGroup> querySpecsByCid(@PathVariable("cid") Long cid);}SpecificationController
@GetMapping("{cid}") public ResponseEntity<List<SpecGroup>> querySpecsByCid(@PathVariable("cid") Long cid){List<SpecGroup> list = this.specificationService.querySpecsByCid(cid);if(list == null || list.size() == 0){return new ResponseEntity<>(HttpStatus.NOT_FOUND);}return ResponseEntity.ok(list); }SpecificationService
public List<SpecGroup> querySpecsByCid(Long cid) {// 查詢規格組List<SpecGroup> groups = this.querySpecGroups(cid);groups.forEach(g -> {// 查詢組內參數g.setParams(this.querySpecParams(g.getId(), null, null, null));});return groups; }在service中,我們調用之前編寫過的方法,查詢規格組,和規格參數,然后封裝返回。
總結
以上是生活随笔為你收集整理的商品微服务添加api接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搭建商品详情页工程
- 下一篇: 组织商品详情页的数据模型