Browse Source

集成knife4j

main
liuchen864 2 years ago
parent
commit
f4a728ec8d
  1. 3
      .gitignore
  2. 6
      dc-admin/pom.xml
  3. 2
      dc-admin/src/main/java/com/dc/web/DcApplication.java
  4. 50
      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. 8
      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

3
.gitignore

@ -3,3 +3,6 @@
*/target/
*.imi
*.DS_Store
logs
*.log

6
dc-admin/pom.xml

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

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

@ -1,6 +1,7 @@
package com.dc.web;
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.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
*
* @author win
*/
@EnableKnife4j
@SpringBootApplication(
scanBasePackages = { "com.dc.common", "com.dc.system", "com.dc.framework", "com.dc.generator", "com.dc.quartz", "com.dc.web"},
exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})

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

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

20
dc-app/pom.xml

@ -24,25 +24,19 @@
<optional>true</optional> <!-- 表示依赖不会传递 -->
</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驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Postgresql驱动包 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 核心模块-->
<dependency>
<groupId>com.dc</groupId>

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

@ -1,6 +1,7 @@
package com.dc.app;
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.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@ -11,13 +12,12 @@ import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfi
*
* @author win
*/
@EnableKnife4j
@SpringBootApplication(
scanBasePackages = {"com.dc.system", "com.dc.framework", "com.dc.common", "com.dc.app"},
exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class, SecurityAutoConfiguration.class})
public class AppApplication
{
public static void main(String[] args)
{
public class AppApplication {
public static void main(String[] args) {
// System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(AppApplication.class, args);
System.out.println("闻音启动成功");

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

@ -1,7 +1,6 @@
package com.dc.app.config;
import com.dc.common.config.WinConfig;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -51,10 +50,8 @@ public class SwaggerConfig
.apiInfo(apiInfo())
// 设置哪些接口暴露给Swagger展示
.select()
// 扫描所有有注解的api,用这种方式更灵活
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// 扫描指定包中的swagger注解
// .apis(RequestHandlerSelectors.basePackage("com.dc.project.tool.swagger"))
.apis(RequestHandlerSelectors.basePackage("com.dc.app.controller"))
// 扫描所有 .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.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) {
registry.addInterceptor(localInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui.html/**");
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui.html/**", "/doc.html");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");

5
dc-system/pom.xml

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

28
pom.xml

@ -20,7 +20,6 @@
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<druid.version>1.2.16</druid.version>
<bitwalker.version>1.21</bitwalker.version>
<swagger.version>3.0.0</swagger.version>
<kaptcha.version>2.3.3</kaptcha.version>
<pagehelper.boot.version>1.4.6</pagehelper.boot.version>
<fastjson.version>2.0.34</fastjson.version>
@ -34,6 +33,7 @@
<mybatis-plus.version>3.5.3</mybatis-plus.version>
<shardingsphere.version>5.1.1</shardingsphere.version>
<dynamic-datasource.version>3.3.2</dynamic-datasource.version>
<knife4j.version>3.0.3</knife4j.version>
</properties>
<!-- 依赖声明 -->
@ -84,26 +84,6 @@
<version>${oshi.version}</version>
</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常用工具类 -->
<dependency>
<groupId>commons-io</groupId>
@ -206,6 +186,12 @@
<version>${dynamic-datasource.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<!-- 定时任务-->
<dependency>
<groupId>com.dc</groupId>

Loading…
Cancel
Save