Browse Source

集成knife4j

main
liuchen864 2 years ago
parent
commit
f4a728ec8d
  1. 5
      .gitignore
  2. 6
      dc-admin/pom.xml
  3. 2
      dc-admin/src/main/java/com/dc/web/DcApplication.java
  4. 52
      dc-admin/src/main/java/com/dc/web/config/SwaggerConfig.java
  5. 2
      dc-admin/src/main/java/com/dc/web/controller/tool/SwaggerController.java
  6. 20
      dc-app/pom.xml
  7. 14
      dc-app/src/main/java/com/dc/app/AppApplication.java
  8. 5
      dc-app/src/main/java/com/dc/app/config/SwaggerConfig.java
  9. 4
      dc-app/src/main/java/com/dc/app/config/WebConfig.java
  10. 5
      dc-system/pom.xml
  11. 28
      pom.xml

5
.gitignore

@ -2,4 +2,7 @@
*.gitignore *.gitignore
*/target/ */target/
*.imi *.imi
*.DS_Store *.DS_Store
logs
*.log

6
dc-admin/pom.xml

@ -24,12 +24,6 @@
<optional>true</optional> <!-- 表示依赖不会传递 --> <optional>true</optional> <!-- 表示依赖不会传递 -->
</dependency> </dependency>
<!-- swagger3-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
<!-- Mysql驱动包 --> <!-- Mysql驱动包 -->
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>

2
dc-admin/src/main/java/com/dc/web/DcApplication.java

@ -1,6 +1,7 @@
package com.dc.web; package com.dc.web;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
* *
* @author win * @author win
*/ */
@EnableKnife4j
@SpringBootApplication( @SpringBootApplication(
scanBasePackages = { "com.dc.common", "com.dc.system", "com.dc.framework", "com.dc.generator", "com.dc.quartz", "com.dc.web"}, scanBasePackages = { "com.dc.common", "com.dc.system", "com.dc.framework", "com.dc.generator", "com.dc.quartz", "com.dc.web"},
exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class}) exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})

52
dc-admin/src/main/java/com/dc/web/config/SwaggerConfig.java

@ -1,7 +1,6 @@
package com.dc.web.config; package com.dc.web.config;
import com.dc.common.config.WinConfig; import com.dc.common.config.WinConfig;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In; import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -20,55 +19,55 @@ import java.util.List;
/** /**
* Swagger2的接口配置 * Swagger2的接口配置
* *
* @author win * @author win
*/ */
@Configuration @Configuration
public class SwaggerConfig public class SwaggerConfig {
{ /**
/** 系统基础配置 */ * 系统基础配置
*/
@Autowired @Autowired
private WinConfig winConfig; private WinConfig winConfig;
/** 是否开启swagger */ /**
* 是否开启swagger
*/
@Value("${swagger.enabled}") @Value("${swagger.enabled}")
private boolean enabled; private boolean enabled;
/** 设置请求的统一前缀 */ /**
* 设置请求的统一前缀
*/
@Value("${swagger.pathMapping}") @Value("${swagger.pathMapping}")
private String pathMapping; private String pathMapping;
/** /**
* 创建API * 创建API
*/ */
@Bean @Bean(value = "dockerBean")
public Docket createRestApi() public Docket dockerBean() {
{ //指定使用Swagger2规范
return new Docket(DocumentationType.OAS_30) Docket docket = new Docket(DocumentationType.SWAGGER_2)
// 是否启用Swagger
.enable(enabled)
// 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
.apiInfo(apiInfo()) .apiInfo(apiInfo())
// 设置哪些接口暴露给Swagger展示 //分组名称
.groupName("用户服务")
.select() .select()
// 扫描所有有注解的api,用这种方式更灵活 //这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .apis(RequestHandlerSelectors.basePackage("com.dc.app.controller"))
// 扫描指定包中的swagger注解
// .apis(RequestHandlerSelectors.basePackage("com.dc.project.tool.swagger"))
// 扫描所有 .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()) .paths(PathSelectors.any())
.build() .build()
/* 设置安全模式,swagger可以设置访问token */ /* 设置安全模式,swagger可以设置访问token */
.securitySchemes(securitySchemes()) .securitySchemes(securitySchemes())
.securityContexts(securityContexts()) .securityContexts(securityContexts())
.pathMapping(pathMapping); .pathMapping(pathMapping);
return docket;
} }
/** /**
* 安全模式这里指定token通过Authorization头请求头传递 * 安全模式这里指定token通过Authorization头请求头传递
*/ */
private List<SecurityScheme> securitySchemes() private List<SecurityScheme> securitySchemes() {
{
List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>(); List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();
apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue())); apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue()));
return apiKeyList; return apiKeyList;
@ -77,8 +76,7 @@ public class SwaggerConfig
/** /**
* 安全上下文 * 安全上下文
*/ */
private List<SecurityContext> securityContexts() private List<SecurityContext> securityContexts() {
{
List<SecurityContext> securityContexts = new ArrayList<>(); List<SecurityContext> securityContexts = new ArrayList<>();
securityContexts.add( securityContexts.add(
SecurityContext.builder() SecurityContext.builder()
@ -91,8 +89,7 @@ public class SwaggerConfig
/** /**
* 默认的安全上引用 * 默认的安全上引用
*/ */
private List<SecurityReference> defaultAuth() private List<SecurityReference> defaultAuth() {
{
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope; authorizationScopes[0] = authorizationScope;
@ -104,8 +101,7 @@ public class SwaggerConfig
/** /**
* 添加摘要信息 * 添加摘要信息
*/ */
private ApiInfo apiInfo() private ApiInfo apiInfo() {
{
// 用ApiInfoBuilder进行定制 // 用ApiInfoBuilder进行定制
return new ApiInfoBuilder() return new ApiInfoBuilder()
// 设置标题 // 设置标题

2
dc-admin/src/main/java/com/dc/web/controller/tool/SwaggerController.java

@ -17,6 +17,6 @@ public class SwaggerController extends BaseController
@GetMapping() @GetMapping()
public String index() public String index()
{ {
return redirect("/swagger-ui.html"); return redirect("/doc.html");
} }
} }

20
dc-app/pom.xml

@ -24,25 +24,19 @@
<optional>true</optional> <!-- 表示依赖不会传递 --> <optional>true</optional> <!-- 表示依赖不会传递 -->
</dependency> </dependency>
<!-- swagger3-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
<!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.6.2</version>
</dependency>
<!-- Mysql驱动包 --> <!-- Mysql驱动包 -->
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
</dependency> </dependency>
<!-- Postgresql驱动包 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 核心模块--> <!-- 核心模块-->
<dependency> <dependency>
<groupId>com.dc</groupId> <groupId>com.dc</groupId>

14
dc-app/src/main/java/com/dc/app/AppApplication.java

@ -1,6 +1,7 @@
package com.dc.app; package com.dc.app;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@ -8,16 +9,15 @@ import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfi
/** /**
* 启动程序 * 启动程序
* *
* @author win * @author win
*/ */
@EnableKnife4j
@SpringBootApplication( @SpringBootApplication(
scanBasePackages={"com.dc.system", "com.dc.framework", "com.dc.common", "com.dc.app"}, scanBasePackages = {"com.dc.system", "com.dc.framework", "com.dc.common", "com.dc.app"},
exclude = { DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class, SecurityAutoConfiguration.class}) exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class, SecurityAutoConfiguration.class})
public class AppApplication public class AppApplication {
{ public static void main(String[] args) {
public static void main(String[] args)
{
// System.setProperty("spring.devtools.restart.enabled", "false"); // System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(AppApplication.class, args); SpringApplication.run(AppApplication.class, args);
System.out.println("闻音启动成功"); System.out.println("闻音启动成功");

5
dc-app/src/main/java/com/dc/app/config/SwaggerConfig.java

@ -1,7 +1,6 @@
package com.dc.app.config; package com.dc.app.config;
import com.dc.common.config.WinConfig; import com.dc.common.config.WinConfig;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In; import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -51,10 +50,8 @@ public class SwaggerConfig
.apiInfo(apiInfo()) .apiInfo(apiInfo())
// 设置哪些接口暴露给Swagger展示 // 设置哪些接口暴露给Swagger展示
.select() .select()
// 扫描所有有注解的api,用这种方式更灵活
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// 扫描指定包中的swagger注解 // 扫描指定包中的swagger注解
// .apis(RequestHandlerSelectors.basePackage("com.dc.project.tool.swagger")) .apis(RequestHandlerSelectors.basePackage("com.dc.app.controller"))
// 扫描所有 .apis(RequestHandlerSelectors.any()) // 扫描所有 .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()) .paths(PathSelectors.any())
.build() .build()

4
dc-app/src/main/java/com/dc/app/config/WebConfig.java

@ -21,12 +21,12 @@ public class WebConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localInterceptor()) registry.addInterceptor(localInterceptor())
.addPathPatterns("/**") .addPathPatterns("/**")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui.html/**"); .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui.html/**", "/doc.html");
} }
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html") registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/"); .addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**") registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/"); .addResourceLocations("classpath:/META-INF/resources/webjars/");

5
dc-system/pom.xml

@ -17,10 +17,9 @@
<dependencies> <dependencies>
<!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
<dependency> <dependency>
<groupId>io.swagger</groupId> <groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-models</artifactId> <artifactId>knife4j-spring-boot-starter</artifactId>
</dependency> </dependency>
<!-- 通用工具--> <!-- 通用工具-->

28
pom.xml

@ -20,7 +20,6 @@
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<druid.version>1.2.16</druid.version> <druid.version>1.2.16</druid.version>
<bitwalker.version>1.21</bitwalker.version> <bitwalker.version>1.21</bitwalker.version>
<swagger.version>3.0.0</swagger.version>
<kaptcha.version>2.3.3</kaptcha.version> <kaptcha.version>2.3.3</kaptcha.version>
<pagehelper.boot.version>1.4.6</pagehelper.boot.version> <pagehelper.boot.version>1.4.6</pagehelper.boot.version>
<fastjson.version>2.0.34</fastjson.version> <fastjson.version>2.0.34</fastjson.version>
@ -34,6 +33,7 @@
<mybatis-plus.version>3.5.3</mybatis-plus.version> <mybatis-plus.version>3.5.3</mybatis-plus.version>
<shardingsphere.version>5.1.1</shardingsphere.version> <shardingsphere.version>5.1.1</shardingsphere.version>
<dynamic-datasource.version>3.3.2</dynamic-datasource.version> <dynamic-datasource.version>3.3.2</dynamic-datasource.version>
<knife4j.version>3.0.3</knife4j.version>
</properties> </properties>
<!-- 依赖声明 --> <!-- 依赖声明 -->
@ -84,26 +84,6 @@
<version>${oshi.version}</version> <version>${oshi.version}</version>
</dependency> </dependency>
<!-- Swagger3依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${swagger.version}</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.6.2</version>
</dependency>
<!-- io常用工具类 --> <!-- io常用工具类 -->
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
@ -206,6 +186,12 @@
<version>${dynamic-datasource.version}</version> <version>${dynamic-datasource.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<!-- 定时任务--> <!-- 定时任务-->
<dependency> <dependency>
<groupId>com.dc</groupId> <groupId>com.dc</groupId>

Loading…
Cancel
Save