在 foodie-dev-pojo 模块下的 com.imooc.pojo.vo 包内新建 ItemInfoVO 类

    1. package com.imooc.pojo.vo;
    2. import com.imooc.pojo.Items;
    3. import com.imooc.pojo.ItemsImg;
    4. import com.imooc.pojo.ItemsParam;
    5. import com.imooc.pojo.ItemsSpec;
    6. import java.util.List;
    7. /**
    8. * 商品详情 VO
    9. * Created by 92578 on 2020/8/22 21:28
    10. **/
    11. public class ItemInfoVO {
    12. private Items item;
    13. private List<ItemsImg> itemImgList;
    14. private List<ItemsSpec> itemSpecList;
    15. private ItemsParam itemParams;
    16. public Items getItem() {
    17. return item;
    18. }
    19. public void setItem(Items item) {
    20. this.item = item;
    21. }
    22. public List<ItemsImg> getItemImgList() {
    23. return itemImgList;
    24. }
    25. public void setItemImgList(List<ItemsImg> itemImgList) {
    26. this.itemImgList = itemImgList;
    27. }
    28. public List<ItemsSpec> getItemSpecList() {
    29. return itemSpecList;
    30. }
    31. public void setItemSpecList(List<ItemsSpec> itemSpecList) {
    32. this.itemSpecList = itemSpecList;
    33. }
    34. public ItemsParam getItemParams() {
    35. return itemParams;
    36. }
    37. public void setItemParams(ItemsParam itemParams) {
    38. this.itemParams = itemParams;
    39. }
    40. }

    在 foodie-dev-api 模块下的 com.imooc.controller 包内创建 ItemsController 类

    1. package com.imooc.controller;
    2. import com.imooc.pojo.Items;
    3. import com.imooc.pojo.ItemsImg;
    4. import com.imooc.pojo.ItemsParam;
    5. import com.imooc.pojo.ItemsSpec;
    6. import com.imooc.pojo.vo.ItemInfoVO;
    7. import com.imooc.service.ItemService;
    8. import com.imooc.utils.IMOOCJSONResult;
    9. import io.swagger.annotations.Api;
    10. import io.swagger.annotations.ApiOperation;
    11. import io.swagger.annotations.ApiParam;
    12. import org.apache.commons.lang3.StringUtils;
    13. import org.springframework.beans.factory.annotation.Autowired;
    14. import org.springframework.web.bind.annotation.GetMapping;
    15. import org.springframework.web.bind.annotation.PathVariable;
    16. import org.springframework.web.bind.annotation.RequestMapping;
    17. import org.springframework.web.bind.annotation.RestController;
    18. import java.util.List;
    19. /**
    20. * Created by 92578 on 2020/8/22 16:30
    21. **/
    22. @Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
    23. @RestController
    24. @RequestMapping("items")
    25. public class ItemsController {
    26. @Autowired
    27. private ItemService itemService;
    28. @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    29. @GetMapping("/info/{itemId}")
    30. public IMOOCJSONResult info(
    31. @ApiParam(name = "itemId", value = "商品 id", required = true)
    32. @PathVariable String itemId) {
    33. if (StringUtils.isBlank(itemId)) {
    34. return IMOOCJSONResult.errorMsg(null);
    35. }
    36. Items item = itemService.queryItemById(itemId);
    37. List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
    38. List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
    39. ItemsParam itemsParam = itemService.queryItemsParam(itemId);
    40. ItemInfoVO itemInfoVO = new ItemInfoVO();
    41. itemInfoVO.setItem(item);
    42. itemInfoVO.setItemImgList(itemsImgList);
    43. itemInfoVO.setItemSpecList(itemsSpecList);
    44. itemInfoVO.setItemParams(itemsParam);
    45. return IMOOCJSONResult.ok(itemInfoVO);
    46. }
    47. }

    启动项目,打开浏览器,访问 http://localhost:8088/doc.html
    找到“查询商品详情接口”,输入参数“cake-1001”,点击“发送”,得到返回结果
    image.png
    访问 http://localhost:8080/foodie-shop/item.html?itemId=bingan-1002
    商品详情页能正确显示出来
    image.png