refactor swagger依赖
parent
5e4c44846f
commit
fc685dd637
|
@ -24,28 +24,10 @@
|
|||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!--springfox swagger官方Starter-->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-boot-starter</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-models</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-models</artifactId>
|
||||
<version>1.6.8</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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<Docket> createRestApi(SwaggerProperties swaggerProperties) {
|
||||
List<Docket> 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<RequestParameter> getGlobalRequestParameters() {
|
||||
List<RequestParameter> parameters = new ArrayList<>();
|
||||
parameters.add(new RequestParameterBuilder()
|
||||
.name("token")
|
||||
.description("令牌")
|
||||
.required(false)
|
||||
.in(ParameterType.HEADER)
|
||||
.build());
|
||||
return parameters;
|
||||
}
|
||||
|
||||
private List<Response> getGlobalResponseMessage() {
|
||||
List<Response> responseList = new ArrayList<>();
|
||||
responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
|
||||
return responseList;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<RequestParameter> getGlobalRequestParameters() {
|
||||
List<RequestParameter> 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<Response> getGlobalResponseMessage() {
|
||||
List<Response> responseList = new ArrayList<>();
|
||||
responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
|
||||
return responseList;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -136,7 +136,19 @@
|
|||
<groupId>cc.iotkit</groupId>
|
||||
<artifactId>iot-message-event-bus</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cc.iotkit</groupId>
|
||||
<artifactId>iot-common-doc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cc.iotkit</groupId>
|
||||
<artifactId>iot-manager</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cc.iotkit</groupId>
|
||||
<artifactId>iot-system</artifactId>
|
||||
</dependency>
|
||||
<!--打开注释使用rocketmq消息总线-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>cc.iotkit</groupId>-->
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -42,7 +42,7 @@
|
|||
<velocity.version>2.3</velocity.version>
|
||||
<ip2region.version>2.7.0</ip2region.version>
|
||||
<jap-comment.version>1.0.0</jap-comment.version>
|
||||
<knife4j.version>2.0.5</knife4j.version>
|
||||
<knife4j.version>2.0.9</knife4j.version>
|
||||
<validateion.version>1.1.0.Final</validateion.version>
|
||||
</properties>
|
||||
|
||||
|
|
Loading…
Reference in New Issue