增加 swagger 提供 API 文档功能

增加 mapstruct 提供 Bean 之间的复制
This commit is contained in:
YunaiV 2019-02-22 19:33:42 +08:00
parent aedecc44d1
commit 398d23cc9f
23 changed files with 633 additions and 38 deletions

View File

@ -13,7 +13,6 @@
<packaging>pom</packaging>
<modules>
<module>product-application</module>
<module>product-service</module>
<module>product-service-api</module>
</modules>

View File

@ -11,6 +11,10 @@
<artifactId>product-rest</artifactId>
<properties>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>cn.iocoder.mall</groupId>
@ -60,6 +64,46 @@
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher -->
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 提供给 mapstruct 使用 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source> <!-- or higher, depending on your project -->
<target>1.8</target> <!-- or higher, depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,13 @@
package cn.iocoder.mall.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
}

View File

@ -1,20 +0,0 @@
package cn.iocoder.mall.product;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import javax.sql.DataSource;
@SpringBootApplication
@MapperScan("cn.iocoder.mall.product.dao") // 扫描对应的 Mapper 接口
public class ProductRestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(ProductRestApplication.class, args);
DataSource ds = ctx.getBean(DataSource.class);
System.out.println(ds);
}
}

View File

@ -0,0 +1,70 @@
package cn.iocoder.mall.product.bo;
/**
* 商品分类 BO
*/
public class ProductCategoryBO {
/**
* 分类编号
*/
private Integer id;
/**
* 父分类编号
*
* 如果不存在父级 pid = 0
*/
private Integer pid;
/**
* 名称
*/
private String name;
/**
* 分类图片
*/
private String picUrl;
/**
* 排序值
*/
private Integer sort;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
}

View File

@ -0,0 +1,14 @@
package cn.iocoder.mall.product.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@MapperScan("cn.iocoder.mall.product.dao") // 扫描对应的 Mapper 接口
@EnableTransactionManagement(proxyTargetClass = true) // 启动事务管理为什么使用 proxyTargetClass 参数参见 https://blog.csdn.net/huang_550/article/details/76492600
public class DatabaseConfiguration {
// 数据源使用 HikariCP
}

View File

@ -0,0 +1,36 @@
package cn.iocoder.mall.product.config;
import cn.iocoder.mall.product.constants.ErrorCodeEnum;
import cn.iocoder.mall.product.exception.ServiceException;
import cn.iocoder.mall.product.vo.RestResult;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(value = ServiceException.class)
public RestResult serviceExceptionHandler(HttpServletRequest req, Exception e) {
ServiceException ex = (ServiceException) e;
return RestResult.error(ex.getCode(), ex.getMessage());
}
@ResponseBody
@ExceptionHandler(value = Exception.class)
public RestResult resultExceptionHandler(HttpServletRequest req, Exception e) {
// TODO 异常日志
e.printStackTrace();
// TODO 翻译不同的异常
if (e instanceof MissingServletRequestParameterException) {
return RestResult.error(ErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getCode(), ErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getMessage());
}
// 返回
return RestResult.error(ErrorCodeEnum.SYS_ERROR.getCode(), ErrorCodeEnum.SYS_ERROR.getMessage());
}
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.mall.product.config;
import cn.iocoder.mall.product.vo.RestResult;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//@ControllerAdvice
public class GlobalResponseBodyAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true; // TODO 芋艿未来这里可以剔除掉一些需要特殊返回的接口
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof RestResult) {
return body;
}
return RestResult.ok(body);
}
}

View File

@ -0,0 +1,23 @@
package cn.iocoder.mall.product.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebMvc
@Configuration
public class MVCConfiguration implements WebMvcConfigurer {
// @Autowired
// private SecurityInterceptor securityInterceptor;
// @Reference
// private OAuth2Service oauth2Service;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// registry.addInterceptor(securityInterceptor);
}
}

View File

@ -0,0 +1,36 @@
package cn.iocoder.mall.product.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.iocoder.mall.product.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("商品子系统")
.description("商品子系统")
.termsOfServiceUrl("http://www.iocoder.cn")
.version("1.0.0")
.build();
}
}

View File

@ -0,0 +1,32 @@
package cn.iocoder.mall.product.constants;
/**
* 错误码枚举类
*
* 系统级异常使用 2-001-000-000
*/
public enum ErrorCodeEnum {
SYS_ERROR(2001001000, "服务端发生异常"),
MISSING_REQUEST_PARAM_ERROR(2001001001, "参数缺失"),
;
private final int code;
private final String message;
ErrorCodeEnum(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
;
}

View File

@ -1,22 +1,34 @@
package cn.iocoder.mall.product.controller.user;
import cn.iocoder.mall.product.convert.ProductCategoryConvert;
import cn.iocoder.mall.product.service.ProductCategoryService;
import cn.iocoder.mall.product.vo.ProductCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("user/product/category")
@Api("商品分类")
public class ProductCategoryController {
// TODO 获得父编号为 id 的分类们 后面使用 swagger 注释
@Autowired
private ProductCategoryService productCategoryService;
@GetMapping
public List<ProductCategoryVO> list(@RequestParam("id") Integer id) {
return new ArrayList<>();
@ApiOperation("获得指定编号下的子分类的数组")
@ApiImplicitParam(name = "pid", value = "指定分类编号", required = true)
public List<ProductCategoryVO> list(@RequestParam("pid") Integer pid) {
return ProductCategoryConvert.INSTANCE.convertToVO(
productCategoryService.getListByPid(pid)
);
}
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.mall.product.convert;
import cn.iocoder.mall.product.bo.ProductCategoryBO;
import cn.iocoder.mall.product.dataobject.ProductCategoryDO;
import cn.iocoder.mall.product.vo.ProductCategoryVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface ProductCategoryConvert {
ProductCategoryConvert INSTANCE = Mappers.getMapper(ProductCategoryConvert.class);
@Mappings({})
ProductCategoryBO convertToBO(ProductCategoryDO category);
List<ProductCategoryBO> convertToBO(List<ProductCategoryDO> categoryList);
@Mappings({})
ProductCategoryVO convertToVO(ProductCategoryBO category);
List<ProductCategoryVO> convertToVO(List<ProductCategoryBO> categoryList);
}

View File

@ -0,0 +1,17 @@
package cn.iocoder.mall.product.convert;
import cn.iocoder.mall.product.bo.ProductSpuBO;
import cn.iocoder.mall.product.dataobject.ProductSpuDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
@Mapper
public interface ProductSpuConvert {
ProductSpuConvert INSTANCE = Mappers.getMapper(ProductSpuConvert.class);
@Mappings({})
ProductSpuBO convert(ProductSpuDO spu);
}

View File

@ -0,0 +1,15 @@
package cn.iocoder.mall.product.dao;
import cn.iocoder.mall.product.dataobject.ProductCategoryDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductCategoryMapper {
List<ProductCategoryDO> selectListByPidAndStatusOrderBySort(@Param("pid") Integer pid,
@Param("status") Integer status);
}

View File

@ -7,6 +7,8 @@ import java.util.Date;
*/
public class ProductCategoryDO {
public static final Integer STATUS_ENABLE = 1;
/**
* 分类编号
*/
@ -28,7 +30,7 @@ public class ProductCategoryDO {
/**
* 分类图片
*/
private String picURL;
private String picUrl;
/**
* 排序值
*/
@ -49,4 +51,76 @@ public class ProductCategoryDO {
*/
private Integer status;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}

View File

@ -0,0 +1,46 @@
package cn.iocoder.mall.product.exception;
/**
* 服务异常
*
* 参考 https://www.kancloud.cn/onebase/ob/484204 文章
*
* 一共 10 分成四段
*
* 第一段1 类型
* 1 - 业务级别异常
* 2 - 系统级别异常
* 第二段3 系统类型
* 001 - 用户系统
* 002 - 商品系统
* 003 - 订单系统
* 004 - 支付系统
* 005 - 优惠劵系统
* ... - ...
* 第三段3 模块
* 不限制规则
* 一般建议每个系统里面可能有多个模块可以再去做分段以用户系统为例子
* 001 - OAuth2 模块
* 002 - User 模块
* 003 - MobileCode 模块
* 第四段3 错误码
* 不限制规则
* 一般建议每个模块自增
*/
public class ServiceException extends RuntimeException {
/**
* 错误码
*/
private final Integer code;
public ServiceException(Integer code, String message) {
super(message);
this.code = code;
}
public Integer getCode() {
return code;
}
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.mall.product.service;
import cn.iocoder.mall.product.bo.ProductCategoryBO;
import cn.iocoder.mall.product.convert.ProductCategoryConvert;
import cn.iocoder.mall.product.dao.ProductCategoryMapper;
import cn.iocoder.mall.product.dataobject.ProductCategoryDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service // 实际上不用添加添加的原因是必须 Spring 报错提示
//@com.alibaba.dubbo.config.annotation.Service
public class ProductCategoryService {
@Autowired
private ProductCategoryMapper productCategoryMapper;
public List<ProductCategoryBO> getListByPid(Integer pid) {
List<ProductCategoryDO> categoryList = productCategoryMapper.selectListByPidAndStatusOrderBySort(pid, ProductCategoryDO.STATUS_ENABLE);
return ProductCategoryConvert.INSTANCE.convertToBO(categoryList);
}
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.mall.product.service;
import cn.iocoder.mall.product.bo.ProductSpuBO;
import cn.iocoder.mall.product.convert.ProductSpuConvert;
import cn.iocoder.mall.product.dao.ProductSpuMapper;
import cn.iocoder.mall.product.dataobject.ProductSpuDO;
import org.springframework.beans.factory.annotation.Autowired;
@ -15,9 +16,8 @@ public class ProductSpuService implements cn.iocoder.mall.product.service.api.Pr
public ProductSpuBO getProductSpu(Integer id) {
ProductSpuDO productSpuDO = productSpuDAO.selectById(id);
ProductSpuBO productSpuBO = new ProductSpuBO(); // TODO 芋艿后面改下
productSpuBO.setId(productSpuDO.getId());
return productSpuBO;
// 转换成 BO
return ProductSpuConvert.INSTANCE.convert(productSpuDO);
}
}

View File

@ -1,4 +1,40 @@
package cn.iocoder.mall.product.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("商品分类")
public class ProductCategoryVO {
@ApiModelProperty(value = "分类编号", required = true, example = "1")
private Integer id;
@ApiModelProperty(value = "分类名", required = true, example = "手机")
private String name;
@ApiModelProperty(value = "分类图片", notes = "一般情况下,只有根分类才有图片", example = "http://www.iocoder.cn/images/common/wechat_mp_2017_07_31_bak.jpg")
private String picUrl;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
}

View File

@ -0,0 +1,56 @@
package cn.iocoder.mall.product.vo;
public class RestResult {
/**
* 错误码
*/
private Integer code;
/**
* 错误提示
*/
private String message;
/**
* 返回数据
*/
private Object data;
public static RestResult error(Integer code, String message) {
RestResult result = new RestResult();
result.code = code;
result.message = message;
return result;
}
public static RestResult ok(Object data) {
RestResult result = new RestResult();
result.code = 0;
result.data = data;
result.message = "";
return result;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}

View File

@ -1,12 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.mall.product.dao.ProductSpuMapper">
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.iocoder.mall.product.dao.ProductCategoryMapper">
<select id="selectById" parameterType="Integer" resultType="ProductSpuDO">
<select id="selectListByPidAndStatusOrderBySort" resultType="ProductCategoryDO">
SELECT
id
FROM product_spu
WHERE id = #{id}
id, name, pic_url, sort
FROM product_category
WHERE pid = #{pid}
AND status = #{status}
ORDER BY sort ASC
</select>
</mapper>
</mapper>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.mall.product.dao.ProductSpuMapper">
<select id="selectById" parameterType="Integer" resultType="ProductSpuDO">
SELECT
id
FROM product_spu
WHERE id = #{id}
</select>
</mapper>