Skip to content

Commit c7f626a

Browse files
author
Janche
committed
🚀 优化swagger接文档,界面采用knife4j风格
1 parent ee3b619 commit c7f626a

File tree

5 files changed

+20
-26
lines changed

5 files changed

+20
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
3. spring-security目前使用的是基于权限的动态校验,采用黑名单的方式对接口权限的判断。
88
4. 框架内已包含许多工具类,MD5加密、json、POI的excel文档操作,以及时间和日期等常见工具类。
99
5. 采用了@Log对项目日志的记录。
10-
6. 接口文档地址:http://localhost:9999/swagger-ui.html
10+
6. 接口文档地址:http://localhost:9999/doc.html(采用knife4j的文档风格)
1111
7. 加入对OAuth2的支持 ([博客地址](https://blog.csdn.net/qq_34997906/article/details/89600076)
1212

1313
|依赖 |版本 |

swagger/pom.xml

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,11 @@
1111

1212
<artifactId>swagger</artifactId>
1313
<dependencies>
14+
<!-- knife4j 替代swagger显示接口文档 -->
1415
<dependency>
15-
<groupId>io.springfox</groupId>
16-
<artifactId>springfox-swagger2</artifactId>
17-
<version>2.8.0</version>
18-
<exclusions>
19-
<exclusion>
20-
<groupId>com.google.guava</groupId>
21-
<artifactId>guava</artifactId>
22-
</exclusion>
23-
<exclusion>
24-
<groupId>org.javassist</groupId>
25-
<artifactId>javassist</artifactId>
26-
</exclusion>
27-
</exclusions>
28-
</dependency>
29-
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
30-
<dependency>
31-
<groupId>io.springfox</groupId>
32-
<artifactId>springfox-swagger-ui</artifactId>
33-
<version>2.8.0</version>
16+
<groupId>com.github.xiaoymin</groupId>
17+
<artifactId>knife4j-spring-boot-starter</artifactId>
18+
<version>2.0.5</version>
3419
</dependency>
3520
</dependencies>
3621

swagger/src/main/java/com/example/janche/swagger/SwaggerConfig.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.example.janche.swagger;
22

3+
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
34
import org.springframework.beans.factory.annotation.Autowired;
45
import org.springframework.boot.context.properties.EnableConfigurationProperties;
56
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.Import;
9+
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
710
import springfox.documentation.builders.ApiInfoBuilder;
811
import springfox.documentation.builders.PathSelectors;
912
import springfox.documentation.builders.RequestHandlerSelectors;
@@ -14,11 +17,13 @@
1417
import springfox.documentation.swagger2.annotations.EnableSwagger2;
1518

1619
@Configuration
17-
@EnableSwagger2
20+
@EnableKnife4j
21+
@Import(BeanValidatorPluginsConfiguration.class)
1822
@EnableConfigurationProperties(SwaggerProperties.class)
1923
public class SwaggerConfig {
2024
@Autowired
2125
private SwaggerProperties swaggerProperties;
26+
2227
@Bean
2328
public Docket api() {
2429
return new Docket(DocumentationType.SWAGGER_2)
@@ -32,15 +37,15 @@ public Docket api() {
3237

3338
private ApiInfo apiInfo() {
3439
ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
35-
if(swaggerProperties.getTitle()!=null){
40+
if (swaggerProperties.getTitle() != null) {
3641
apiInfoBuilder.title(swaggerProperties.getTitle());
3742
}
38-
if(swaggerProperties.getContractName()!=null&&swaggerProperties.getContractUrl()!=null&&
39-
swaggerProperties.getContractEmail()!=null){
43+
if (swaggerProperties.getContractName() != null && swaggerProperties.getContractUrl() != null
44+
&& swaggerProperties.getContractEmail() != null) {
4045
apiInfoBuilder.contact(new Contact(swaggerProperties.getContractName(), swaggerProperties.getContractUrl(),
4146
swaggerProperties.getContractEmail()));
4247
}
43-
if(swaggerProperties.getVersion()!=null){
48+
if (swaggerProperties.getVersion() != null) {
4449
apiInfoBuilder.version(swaggerProperties.getVersion());
4550
}
4651
return apiInfoBuilder.build();

web/src/main/resources/application-dev.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ mapper:
108108
# 日志
109109
logging.config: classpath:logging-spring.xml
110110
swagger:
111-
title: 接口文档
111+
title: 脚手架接口文档
112112
basepackage: com.example.janche
113113
version: 1.0.0
114+
contract-name: Janche
115+
contract-url: https://blog.csdn.net/qq_34997906
116+
contract-email: [email protected]
114117

web/src/main/resources/application-sysconfig.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ custom:
3232
- "/v2/**"
3333
- "/webjars/**"
3434
- "/swagger-resources/**"
35+
- "/doc.html"
3536

3637
# 热部署插件指定端口,具体视自己情况而定,只要不重复就可以,默认是35729
3738
spring:

0 commit comments

Comments
 (0)