在 foodie-dev-pojo 模块下的 com.imooc.pojo.vo 包内新建 ItemInfoVO 类
package com.imooc.pojo.vo;
import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import java.util.List;
/**
* 商品详情 VO
* Created by 92578 on 2020/8/22 21:28
**/
public class ItemInfoVO {
private Items item;
private List<ItemsImg> itemImgList;
private List<ItemsSpec> itemSpecList;
private ItemsParam itemParams;
public Items getItem() {
return item;
}
public void setItem(Items item) {
this.item = item;
}
public List<ItemsImg> getItemImgList() {
return itemImgList;
}
public void setItemImgList(List<ItemsImg> itemImgList) {
this.itemImgList = itemImgList;
}
public List<ItemsSpec> getItemSpecList() {
return itemSpecList;
}
public void setItemSpecList(List<ItemsSpec> itemSpecList) {
this.itemSpecList = itemSpecList;
}
public ItemsParam getItemParams() {
return itemParams;
}
public void setItemParams(ItemsParam itemParams) {
this.itemParams = itemParams;
}
}
在 foodie-dev-api 模块下的 com.imooc.controller 包内创建 ItemsController 类
package com.imooc.controller;
import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.pojo.vo.ItemInfoVO;
import com.imooc.service.ItemService;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by 92578 on 2020/8/22 16:30
**/
@Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
@RestController
@RequestMapping("items")
public class ItemsController {
@Autowired
private ItemService itemService;
@ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
@GetMapping("/info/{itemId}")
public IMOOCJSONResult info(
@ApiParam(name = "itemId", value = "商品 id", required = true)
@PathVariable String itemId) {
if (StringUtils.isBlank(itemId)) {
return IMOOCJSONResult.errorMsg(null);
}
Items item = itemService.queryItemById(itemId);
List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
ItemsParam itemsParam = itemService.queryItemsParam(itemId);
ItemInfoVO itemInfoVO = new ItemInfoVO();
itemInfoVO.setItem(item);
itemInfoVO.setItemImgList(itemsImgList);
itemInfoVO.setItemSpecList(itemsSpecList);
itemInfoVO.setItemParams(itemsParam);
return IMOOCJSONResult.ok(itemInfoVO);
}
}
启动项目,打开浏览器,访问 http://localhost:8088/doc.html
找到“查询商品详情接口”,输入参数“cake-1001”,点击“发送”,得到返回结果
访问 http://localhost:8080/foodie-shop/item.html?itemId=bingan-1002
商品详情页能正确显示出来