增加 swagger starter

This commit is contained in:
YunaiV 2020-04-20 08:29:09 +08:00
parent 220984c45b
commit 57145d426e
15 changed files with 266 additions and 26 deletions

View File

@ -11,18 +11,6 @@
<artifactId>common-framework</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>common-dependencies</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Web 相关 -->
<dependency>

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>common</artifactId>
<groupId>cn.iocoder.mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mall-spring-boot-starter-swagger</artifactId>
<dependencies>
<!-- Spring 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Web 相关 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,57 @@
package cn.iocoder.mall.swagger.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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;
/**
* 简单的 Swagger2 自动配置类
*
* 较为完善的可以了解 https://mvnrepository.com/artifact/com.spring4all/spring-boot-starter-swagger
*/
@Configuration
@EnableSwagger2
@EnableKnife4j
@ConditionalOnClass({Docket.class, ApiInfoBuilder.class})
@ConditionalOnProperty(prefix = "swagger", value = "enable", matchIfMissing = true) // 允许使用 swagger.enable=false 禁用 Swagger
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public SwaggerProperties swaggerProperties() {
return new SwaggerProperties();
}
@Bean
public Docket createRestApi() {
SwaggerProperties properties = swaggerProperties();
// 创建 Docket 对象
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo(properties))
.select()
.apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(SwaggerProperties properties) {
return new ApiInfoBuilder()
.title(properties.getTitle())
.description(properties.getDescription())
.version(properties.getVersion())
.build();
}
}

View File

@ -0,0 +1,48 @@
package cn.iocoder.mall.swagger.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("swagger")
public class SwaggerProperties {
private String title;
private String description;
private String version;
private String basePackage;
public String getTitle() {
return title;
}
public SwaggerProperties setTitle(String title) {
this.title = title;
return this;
}
public String getDescription() {
return description;
}
public SwaggerProperties setDescription(String description) {
this.description = description;
return this;
}
public String getVersion() {
return version;
}
public SwaggerProperties setVersion(String version) {
this.version = version;
return this;
}
public String getBasePackage() {
return basePackage;
}
public SwaggerProperties setBasePackage(String basePackage) {
this.basePackage = basePackage;
return this;
}
}

View File

@ -0,0 +1 @@
package cn.iocoder.mall.swagger;

View File

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.iocoder.mall.swagger.config.SwaggerAutoConfiguration

View File

@ -26,6 +26,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
@ConditionalOnClass({Docket.class, ApiInfoBuilder.class})
@ConditionalOnProperty(prefix = "swagger", value = "enable", matchIfMissing = true) // 允许使用 swagger.enable=false 禁用 Swagger
@EnableConfigurationProperties(SwaggerProperties.class)
@Deprecated
public class SwaggerAutoConfiguration {
@Bean

View File

@ -5,6 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties("swagger")
@Deprecated
public class SwaggerProperties {
private String title;

View File

@ -15,7 +15,19 @@
<module>common-framework</module>
<module>mall-spring-boot</module>
<module>common-dependencies</module>
<module>mall-spring-boot-starter-swagger</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>mall-dependencies</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

92
mall-dependencies/pom.xml Normal file
View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>onemall</artifactId>
<groupId>cn.iocoder.mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mall-dependencies</artifactId>
<packaging>pom</packaging>
<name>Onemall Dependencies</name>
<description>Maven Bom定义 Onemall 项目的所有依赖的版本</description>
<!-- 属性 -->
<properties>
<!-- TODO Spring Boot && Spring Cloud && Spring Cloud Alibaba -->
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
<!-- Web 相关 -->
<knife4j.version>2.0.2</knife4j.version>
<!-- RPC 相关 -->
<dubbo.version>2.7.1</dubbo.version>
<!-- Transaction 相关 -->
<seata.version>1.1.0</seata.version>
<!-- 监控相关 -->
<skywalking.version>7.0.0</skywalking.version>
</properties>
<!-- 依赖管理 -->
<dependencyManagement>
<!-- TODO Spring Boot && Spring Cloud && Spring Cloud Alibaba -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Web 相关 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<!-- RPC 相关 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- Transaction 相关 -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>${seata.version}</version>
</dependency>
<!-- 监控相关 -->
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
<version>${skywalking.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -24,6 +24,7 @@
<module>promotion</module>
<module>search</module>
<module>demo</module>
<module>mall-dependencies</module>
</modules>
<packaging>pom</packaging>

View File

@ -29,6 +29,11 @@ public interface ProductSpuConvert {
})
ProductSpuBO convert(ProductSpuDO spu);
@Named("translatePicUrlsFromString")
default List<String> translatePicUrlsFromString(String picUrls) {
return StringUtil.split(picUrls, ",");
}
@Mappings({})
List<ProductSpuBO> convert(List<ProductSpuDO> spus);
@ -125,9 +130,6 @@ public interface ProductSpuConvert {
return spuDetailList;
}
@Named("translatePicUrlsFromString")
default List<String> translatePicUrlsFromString(String picUrls) {
return StringUtil.split(picUrls, ",");
}
}

View File

@ -16,8 +16,8 @@
<modules>
<module>system-application</module>
<module>system-sdk</module>
<module>system-service-api</module>
<module>system-service-impl</module>
<!-- <module>system-service-api</module>-->
<!-- <module>system-service-impl</module>-->
<module>system-rpc-api</module>
<module>system-rpc</module>
<module>system-rest</module>

View File

@ -25,15 +25,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- TODO 实现自己的 starter -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<groupId>cn.iocoder.mall</groupId>
<artifactId>mall-spring-boot-starter-swagger</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -3,3 +3,10 @@ server:
port: 18083
servlet:
context-path: /system-api/
# Swagger 配置项
swagger:
title: 管理员子系统
description: 管理员子系统
version: 1.0.0
base-package: cn.iocoder.mall.system.rest.controller