说明:
(1)本篇博客内容:后台的【增加商品】接口;
(2)本篇博客需要注意的点:
● 需要注意:【增加商品】接口:本篇博客还没有完全开发完;这是因为图片的地址,还无法上传;
有关,上传图片的内容,在后面几篇博客介绍;
● 新增商品的时候,也要防止商品名称重名;
目录
1.创建ProductAdminController类;创建增加商品的方法:addProduct()方法;
(1)【url、请求方式:要符合接口规范】,【Swagger文档,添加接口说明】,【返回值要序列化为JSON】;
(2)因为接口参数很多,所以我们额外创建【AddProductRequest】这个bean去接收参数;同时,因为这儿需要参数校验,所以我们使用了Validation参数校验(@Valid);
(3)因为要用到Validation参数校验,所以,需要使用@Valid注解;;因为这儿是POST请求,而且参数是放在Body中的,所以需要使用@RequestBody注解;
(4)有关Service层的listCategoryForCustomer()方法,在下一部分介绍;
(5)【新增商品】接口,其实还没开发完;完整的增加商品,需要上传图片,而这就涉及到了后面要介绍的【上传图片】接口;
(1)在ProductServiceImpl实现类中,编写新增商品的方法add();
(2)然后在ProductService接口中,反向生成方法的声明;
3.在ProductMapper接口中,定义【根据】name查询商品】的方法selectByName();然后在ProductMapper.xml中编写方法的SQL;
(1)在ProductMapper接口中,定义【根据】name查询商品】的方法selectByName();
(2)然后在ProductMapper.xml中编写方法的SQL;
一:【增加商品】接口介绍;
该接口是后台系统的;
(1)【增加商品】接口文档;
(2)【增加商品】接口:在界面上的效果;
(3)【增加商品】接口:和【上传图片】接口的关系;
因为增加商品时候,需要附带上传商品图片;所以,就【增加商品】接口来说,其中的image属性,就是我们【把图片上传到服务器后的,图片的地址】;
而,这个地址,是只要把图片上传到服务器后,才能得到的;
所以,我们在开发【增加商品】接口时,也要开发【上传图片】接口;
【上传图片】接口会把图片上传到服务器,并返回图片在服务器上的地址;然后我们在把这个地址拿过来,作为【增加商品】接口的image参数;
二:正式开发;
1.创建ProductAdminController类;创建增加商品的方法:addProduct()方法;
ProductAdminController类:
package com.imooc.mall.controller; import com.imooc.mall.common.ApiRestResponse; import com.imooc.mall.model.request.AddProductReq; import com.imooc.mall.service.ProductService; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; import javax.validation.Valid; /** * 描述:【商品模块】后台的Controller */ @Controller public class ProductAdminController { @Autowired ProductService productService; @ApiOperation("新增商品") @PostMapping("/admin/product/add") @ResponseBody public ApiRestResponse addProduct(@Valid @RequestBody AddProductReq addProductReq) { productService.add(addProductReq); return ApiRestResponse.success(); } }
说明:
(1)【url、请求方式:要符合接口规范】,【Swagger文档,添加接口说明】,【返回值要序列化为JSON】;
……………………………………………………
(2)因为接口参数很多,所以我们额外创建【AddProductRequest】这个bean去接收参数;同时,因为这儿需要参数校验,所以我们使用了Validation参数校验(@Valid);
AddProductReq类:
package com.imooc.mall.model.request