Skip to content

Commit 62a4265

Browse files
committed
发布:1.1.0.RELEASE
1 parent 5e78362 commit 62a4265

File tree

4 files changed

+66
-18
lines changed

4 files changed

+66
-18
lines changed

README.md

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# spring-boot-starter-swagger
22

3-
该项目主要利用Spring Boot的自动化配置特性来实现快速的将swagger2引入spring boot应用来生成API文档。
3+
该项目主要利用Spring Boot的自动化配置特性来实现快速的将swagger2引入spring boot应用来生成API文档,简化原生使用swagger2的整合代码。
4+
5+
# 版本基础
6+
7+
- Spring Boot:1.5.x
8+
- Swagger:2.7.x
49

510
# 如何使用
611

@@ -12,7 +17,7 @@
1217
<dependency>
1318
<groupId>com.didispace</groupId>
1419
<artifactId>spring-boot-starter-swagger</artifactId>
15-
<version>1.0.0.RELEASE</version>
20+
<version>1.1.0.RELEASE</version>
1621
</dependency>
1722
```
1823

@@ -30,24 +35,59 @@ public class Bootstrap {
3035
}
3136
```
3237

33-
默认情况下就能产生所有Spring MVC加载的请求文档了
38+
默认情况下就能产生所有当前Spring MVC加载的请求映射文档
3439

3540
# 参数配置
3641

37-
更细致的配置文档内容,可以参考如下参数:
42+
更细致的配置内容参考如下:
43+
44+
**配置示例**
45+
46+
```properties
47+
swagger.title=spring-boot-starter-swagger
48+
swagger.description=Starter for swagger 2.x
49+
swagger.version=1.1.0.RELEASE
50+
swagger.license=Apache License, Version 2.0
51+
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
52+
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
53+
swagger.contact.name=程序猿DD
54+
swagger.contact.url=http://blog.didispace.com
55+
swagger.contact.email=dyc87112@qq.com
56+
swagger.base-package=com.didispace
57+
swagger.base-path=/**
58+
swagger.exclude-path=/error, /ops/**
59+
```
60+
61+
**配置说明**
62+
63+
- swagger.title=标题
64+
- swagger.description=描述
65+
- swagger.version=版本
66+
- swagger.license=许可证
67+
- swagger.licenseUrl=许可证URL
68+
- swagger.termsOfServiceUrl=服务条款URL
69+
- swagger.contact.name=维护人
70+
- swagger.contact.url=维护人URL
71+
- swagger.contact.email=维护人email
72+
- swagger.base-package=swagger扫描的基础包,默认:全扫描
73+
- swagger.base-path=需要处理的基础URL规则,默认:/**
74+
- swagger.exclude-path=需要排除的URL规则,默认:空
75+
76+
## Path规则说明
77+
78+
`swagger.base-path``swagger.exclude-path`使用ANT规则配置。
79+
80+
我们可以使用`swagger.base-path`来指定所有需要生成文档的请求路径基础规则,然后再利用`swagger.exclude-path`来剔除部分我们不需要的。
81+
82+
比如,通常我们可以这样设置:
3883

3984
```properties
40-
swagger.title=标题
41-
swagger.description=描述
42-
swagger.version=版本
43-
swagger.license=许可证
44-
swagger.licenseUrl=许可证URL
45-
swagger.termsOfServiceUrl=服务条款URL
46-
swagger.contact.name=维护人
47-
swagger.contact.url=维护人URL
48-
swagger.contact.email=维护人email
49-
swagger.base-path=产生文档的基础URL规则,默认:/**
50-
swagger.exclude-path=需要排除的URL规则,默认为空
85+
management.context-path=/ops
86+
87+
swagger.base-path=/**
88+
swagger.exclude-path=/ops/**, /error
5189
```
5290

53-
`swagger.base-path``swagger.exclude-path`使用ANT规则配置
91+
上面的设置将解析所有除了`/ops/`开始以及spring boot自带`/error`请求路径。
92+
93+
其中,`exclude-path`可以配合`management.context-path=/ops`设置的spring boot actuator的context-path来排除所有监控端点。

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.didispace</groupId>
88
<artifactId>spring-boot-starter-swagger</artifactId>
9-
<version>1.0.0.RELEASE</version>
9+
<version>1.1.0.RELEASE</version>
1010

1111
<name>spring-boot-starter-swagger</name>
1212
<url>https://github.com/dyc87112/spring-boot-starter-swagger</url>
@@ -62,11 +62,13 @@
6262
<groupId>io.springfox</groupId>
6363
<artifactId>springfox-swagger-ui</artifactId>
6464
<version>${version.swagger}</version>
65+
<scope>compile</scope>
6566
</dependency>
6667
<dependency>
6768
<groupId>io.springfox</groupId>
6869
<artifactId>springfox-swagger2</artifactId>
6970
<version>${version.swagger}</version>
71+
<scope>compile</scope>
7072
</dependency>
7173
<dependency>
7274
<groupId>org.projectlombok</groupId>

src/main/java/com/didispace/swagger/SwaggerAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.context.annotation.Configuration;
88
import springfox.documentation.builders.ApiInfoBuilder;
99
import springfox.documentation.builders.PathSelectors;
10+
import springfox.documentation.builders.RequestHandlerSelectors;
1011
import springfox.documentation.service.ApiInfo;
1112
import springfox.documentation.service.Contact;
1213
import springfox.documentation.spi.DocumentationType;
@@ -44,16 +45,17 @@ public Docket createRestApi(SwaggerProperties swaggerProperties) {
4445
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
4546
.build();
4647

48+
// base-path处理
4749
// 当没有配置任何path的时候,解析/**
4850
if(swaggerProperties.getBasePath().isEmpty()) {
4951
swaggerProperties.getBasePath().add("/**");
5052
}
51-
5253
List<Predicate<String>> basePath = new ArrayList();
5354
for(String path : swaggerProperties.getBasePath()) {
5455
basePath.add(PathSelectors.ant(path));
5556
}
5657

58+
// exclude-path处理
5759
List<Predicate<String>> excludePath = new ArrayList();
5860
for(String path : swaggerProperties.getExcludePath()) {
5961
excludePath.add(PathSelectors.ant(path));
@@ -62,6 +64,7 @@ public Docket createRestApi(SwaggerProperties swaggerProperties) {
6264
return new Docket(DocumentationType.SWAGGER_2)
6365
.apiInfo(apiInfo)
6466
.select()
67+
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
6568
.paths(
6669
Predicates.and(
6770
Predicates.not(Predicates.or(excludePath)),

src/main/java/com/didispace/swagger/SwaggerProperties.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public class SwaggerProperties {
3131

3232
private Contact contact = new Contact();
3333

34+
/**swagger会解析的包路径**/
35+
private String basePackage = "";
36+
3437
/**swagger会解析的url规则**/
3538
private List<String> basePath = new ArrayList<>();
3639
/**在basePath基础上需要排除的url规则**/

0 commit comments

Comments
 (0)