From fc685dd63787711d017d810c18db8f743462f32f Mon Sep 17 00:00:00 2001
From: jay <75509151@qq.com>
Date: Thu, 1 Jun 2023 20:22:37 +0800
Subject: [PATCH] =?UTF-8?q?refactor=20swagger=E4=BE=9D=E8=B5=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
iot-common/iot-common-doc/pom.xml | 18 ---
.../annotation/EnableIotKitSwagger2.java | 20 ----
.../config/SwaggerAutoConfiguration.java | 109 +-----------------
.../iotkit/swagger/config/SwaggerConfig.java | 61 +++-------
iot-starter/pom.xml | 12 ++
pom.xml | 2 +-
6 files changed, 34 insertions(+), 188 deletions(-)
delete mode 100644 iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/annotation/EnableIotKitSwagger2.java
diff --git a/iot-common/iot-common-doc/pom.xml b/iot-common/iot-common-doc/pom.xml
index f9679b9c..51f3816b 100644
--- a/iot-common/iot-common-doc/pom.xml
+++ b/iot-common/iot-common-doc/pom.xml
@@ -24,28 +24,10 @@
true
-
-
- io.springfox
- springfox-boot-starter
- 3.0.0
-
-
- io.swagger
- swagger-models
-
-
-
-
- io.swagger
- swagger-models
- 1.6.8
-
com.github.xiaoymin
knife4j-spring-boot-starter
- 3.0.3
diff --git a/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/annotation/EnableIotKitSwagger2.java b/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/annotation/EnableIotKitSwagger2.java
deleted file mode 100644
index f860476a..00000000
--- a/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/annotation/EnableIotKitSwagger2.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package cc.iotkit.swagger.annotation;
-
-import cc.iotkit.swagger.config.SwaggerProperties;
-import org.springframework.context.annotation.Import;
-
-import java.lang.annotation.*;
-
-/**
- * @Author: 石恒
- * @Date: 2023/5/6 22:12
- * @Description:
- */
-@Target({ElementType.TYPE})
-@Retention(RetentionPolicy.RUNTIME)
-@Documented
-@Inherited
-@Import(SwaggerProperties.class)
-public @interface EnableIotKitSwagger2 {
-
-}
diff --git a/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerAutoConfiguration.java b/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerAutoConfiguration.java
index ec9e87ce..418b6285 100644
--- a/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerAutoConfiguration.java
+++ b/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerAutoConfiguration.java
@@ -1,24 +1,7 @@
package cc.iotkit.swagger.config;
-import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
-import io.swagger.annotations.Api;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
-import org.springframework.http.HttpMethod;
-import springfox.documentation.builders.*;
-import springfox.documentation.service.*;
-import springfox.documentation.spi.DocumentationType;
-import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
-import springfox.documentation.spring.web.plugins.Docket;
-import springfox.documentation.swagger2.annotations.EnableSwagger2;
-
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.List;
/**
* @Author: 石恒
@@ -26,95 +9,7 @@ import java.util.List;
* @Description:
*/
@Configuration
-@EnableSwagger2
-@EnableAutoConfiguration
-@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
-@EnableKnife4j
+@ComponentScan(basePackages = {"cc.iotkit.swagger"})
public class SwaggerAutoConfiguration {
- @Bean
- @ConditionalOnMissingBean
- public SwaggerProperties swaggerProperties() {
- return new SwaggerProperties();
- }
-
- @Bean
- public List createRestApi(SwaggerProperties swaggerProperties) {
- List docketList = new LinkedList<>();
- if (swaggerProperties.getDocket().isEmpty()) {
- ApiSelectorBuilder docket = new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo(swaggerProperties))
- .enable(swaggerProperties.getEnabled())
- .select()
- .apis(RequestHandlerSelectors.withClassAnnotation(Api.class));
- if (StringUtils.isNotBlank(swaggerProperties.getBasePackage())) {
- docket.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()));
- }
- swaggerProperties.getBasePath().forEach(p -> docket.paths(PathSelectors.ant(p)));
- swaggerProperties.getExcludePath().forEach(p -> docket.paths(PathSelectors.ant(p).negate()));
- docketList.add(
- docket.build()
- .globalRequestParameters(getGlobalRequestParameters())
- .globalResponses(HttpMethod.GET, getGlobalResponseMessage())
- .globalResponses(HttpMethod.POST, getGlobalResponseMessage()));
- }
- swaggerProperties.getDocket().forEach((k, v) -> {
- SwaggerProperties.DocketInfo docketInfo = swaggerProperties.getDocket().get(k);
- ApiInfo apiInfo = new ApiInfoBuilder()
- //页面标题
- .title(docketInfo.getTitle())
- //创建人
- .contact(new Contact(docketInfo.getContact().getName(),
- docketInfo.getContact().getUrl(),
- docketInfo.getContact().getEmail()))
- .version(docketInfo.getVersion())
- .description(docketInfo.getDescription())
- .build();
- ApiSelectorBuilder docket = new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo)
- .enable(swaggerProperties.getEnabled())
- .groupName(docketInfo.getGroup())
- .select()
- //为当前包路径
- .apis(RequestHandlerSelectors.withClassAnnotation(Api.class));
- if (StringUtils.isNotBlank(docketInfo.getBasePackage())) {
- docket.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()));
- }
- swaggerProperties.getBasePath().forEach(p -> docket.paths(PathSelectors.ant(p)));
- swaggerProperties.getExcludePath().forEach(p -> docket.paths(PathSelectors.ant(p).negate()));
- docketList.add(docket.build());
- });
- return docketList;
- }
-
-
- @Bean
- public ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
- return new ApiInfoBuilder()
- .title(swaggerProperties.getTitle())
- .version(swaggerProperties.getVersion())
- .description(swaggerProperties.getDescription())
- .contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
- .build();
- }
-
- /**
- * 添加head参数配置
- */
- private List getGlobalRequestParameters() {
- List parameters = new ArrayList<>();
- parameters.add(new RequestParameterBuilder()
- .name("token")
- .description("令牌")
- .required(false)
- .in(ParameterType.HEADER)
- .build());
- return parameters;
- }
-
- private List getGlobalResponseMessage() {
- List responseList = new ArrayList<>();
- responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
- return responseList;
- }
}
diff --git a/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerConfig.java b/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerConfig.java
index f80148f6..7cfea208 100644
--- a/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerConfig.java
+++ b/iot-common/iot-common-doc/src/main/java/cc/iotkit/swagger/config/SwaggerConfig.java
@@ -2,14 +2,15 @@ package cc.iotkit.swagger.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.http.HttpMethod;
+import org.springframework.stereotype.Component;
import springfox.documentation.builders.*;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
-import springfox.documentation.swagger2.annotations.EnableSwagger2;
+import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import java.util.ArrayList;
import java.util.List;
@@ -19,53 +20,29 @@ import java.util.List;
* @Date: 2023/5/4 20:12
* @Description:
*/
-@Configuration
-@EnableSwagger2
-@EnableKnife4j
+@Component
+@EnableSwagger2WebMvc
+
public class SwaggerConfig {
- @Bean
- public Docket createApi() {
+ @Value("${spring.application.name:Swagger API}")
+ private String applicationName;
+
+ @Bean(value = "defaultApi2")
+ public Docket defaultApi2() {
return new Docket(DocumentationType.SWAGGER_2)
+ .groupName(applicationName)
.apiInfo(apiInfo())
.select()
- .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
+ .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
- .build()
- .globalRequestParameters(getGlobalRequestParameters())
- .globalResponses(HttpMethod.GET, getGlobalResponseMessage())
- .globalResponses(HttpMethod.POST, getGlobalResponseMessage());
- }
-
- @Bean
- public ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("奇特物联")
- .description("奇特物联")
- .contact(new Contact("奇特物联-开源", "https://gitee.com/iotkit-open-source/iotkit-parent.git", "user@iotkit.com"))
- .version("1.0")
.build();
}
- /**
- * 添加head参数配置
- */
- private List getGlobalRequestParameters() {
- List parameters = new ArrayList<>();
- parameters.add(new RequestParameterBuilder()
- .name("token")
- .description("令牌")
- .required(false)
- .in(ParameterType.HEADER)
- .build());
- return parameters;
+ private ApiInfo apiInfo() {
+ return new ApiInfoBuilder()
+ .title(applicationName)
+ .description("Swagger API Doc")
+ .build();
}
- private List getGlobalResponseMessage() {
- List responseList = new ArrayList<>();
- responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
- return responseList;
- }
-
}
-
-
diff --git a/iot-starter/pom.xml b/iot-starter/pom.xml
index 9ec53c8c..820e14bd 100644
--- a/iot-starter/pom.xml
+++ b/iot-starter/pom.xml
@@ -136,7 +136,19 @@
cc.iotkit
iot-message-event-bus
+
+ cc.iotkit
+ iot-common-doc
+
+
+ cc.iotkit
+ iot-manager
+
+
+ cc.iotkit
+ iot-system
+
diff --git a/pom.xml b/pom.xml
index e198d0d5..d578c4dd 100755
--- a/pom.xml
+++ b/pom.xml
@@ -42,7 +42,7 @@
2.3
2.7.0
1.0.0
- 2.0.5
+ 2.0.9
1.1.0.Final