01-Swagger不同版本集成与聚合

image-20220406214732607

参考资料(Swagger):https://swagger.io/

参考资料(Knife4j):https://doc.xiaominfo.com/

1. 官方Swagger UI

1.1 访问效果

image-20220406220100019

1.2 依赖

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--Swagger 核心依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--Swagger UI-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

1.3 配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

public static final String PROJECT_NAME = "TestSwagger";
public static final String PROJECT_VERSION = "1.0.0";
public static final String PROJECT_URL = "www.swaggertest.com";
public static final String SCAN_API_PACKAGE = "com.jerry.swagger.controller";
public static final String SCAN_API_PATH = "/**";
public static final String CONTACT_NAME = "Jerry(姜源)";
public static final String CONTACT_URL = PROJECT_URL;
public static final String CONTACT_EMAIL = "swagger@163.com";

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage(SCAN_API_PACKAGE))
.paths(PathSelectors.ant(SCAN_API_PATH))
.build()
.apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(PROJECT_NAME + " 项目接口文档")
.description(PROJECT_NAME + " API接口文档")
.version(PROJECT_VERSION)
.contact(new Contact(CONTACT_NAME, CONTACT_URL, CONTACT_EMAIL))
.termsOfServiceUrl(CONTACT_URL)
.license("")
.licenseUrl("")
.build();
}
}

1.4 配置文件

1
2
3
4
5
6
7
server:
port: 8088
#解决Springboot启动报错Failed to start bean 'documentationPluginsBootstrapper'
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher

1.5 测试接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
@Api(tags = "测试接口")
public class TestController {

@ApiOperation(value = "测试", notes = "测试")
@PostMapping("/api")
public String test() {
return "OK";
}
}

1.6 访问路径

访问路径:/swagger-ui.html

接口json:/v2/api-docs

2. 美化BootStrap UI

2.1 访问效果

image-20220406220524190

2.2 依赖

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--Swagger 核心依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger bootstrap UI -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

2.3 配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI //springBoot 配 swagger 使用这个注解,对应 /doc.html
public class SwaggerConfig {

public static final String PROJECT_NAME = "TestSwagger";
public static final String PROJECT_VERSION = "1.0.0";
public static final String PROJECT_URL = "www.swaggertest.com";
public static final String SCAN_API_PACKAGE = "com.jerry.swagger.controller";
public static final String SCAN_API_PATH = "/**";
public static final String CONTACT_NAME = "Jerry(姜源)";
public static final String CONTACT_URL = PROJECT_URL;
public static final String CONTACT_EMAIL = "swagger@163.com";

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage(SCAN_API_PACKAGE))
.paths(PathSelectors.ant(SCAN_API_PATH))
.build()
.apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(PROJECT_NAME + " 项目接口文档")
.description(PROJECT_NAME + " API接口文档")
.version(PROJECT_VERSION)
.contact(new Contact(CONTACT_NAME, CONTACT_URL, CONTACT_EMAIL))
.termsOfServiceUrl(CONTACT_URL)
.license("")
.licenseUrl("")
.build();
}
}

2.4 配置文件

1
2
3
4
5
6
7
server:
port: 8088
#解决Springboot启动报错Failed to start bean 'documentationPluginsBootstrapper'
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher

2.5 测试接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
@Api(tags = "测试接口")
public class TestController {

@ApiOperation(value = "测试", notes = "测试")
@PostMapping("/api")
public String test() {
return "OK";
}
}

2.6 访问路径

访问路径:/doc.html

接口json:/v2/api-docs

3. 增强Knife4j

3.1 访问效果

image-20220406220948735

3.2 依赖

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<!--Swagger 核心依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--Knife4j UI-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>

3.3 配置类

同2.3

3.4 配置文件

同2.4

3.5 测试接口

同2.5

3.6 访问路径

访问路径:/doc.html

接口json:/v2/api-docs

4. 架构师集成Swagger

4.1 访问效果

image-20220406222024224

以最新的 Knife4j 为例,使 swagger 可配置开关。

4.2 依赖

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<!--Swagger 核心依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--Knife4j UI-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>

4.3 自动配置类

SwaggerAutoConfiguration.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@EnableSwagger2
//proxyBeanMethods默认为true,指定@Bean注解的方法使用代理即从IOC中取对象,为false可以提高性能
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({SwaggerProperties.class})
//matchIfMissing=true,没有该配置属性时也会正常加载;反之则不会生效
@ConditionalOnProperty(name = {"swagger.enabled"}, matchIfMissing = true)
@SuppressWarnings("all")
public class SwaggerAutoConfiguration {
/**
* 默认排除的路径
*/
private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**");
private static final String BASE_PATH = "/**";

public SwaggerAutoConfiguration() {
}

@Bean
public Docket api(SwaggerProperties swaggerProperties) {
if (swaggerProperties.getBasePath().isEmpty()) {
swaggerProperties.getBasePath().add("/**");
}

List<Predicate<String>> basePath = new ArrayList();
swaggerProperties.getBasePath().forEach((path) -> {
basePath.add(PathSelectors.ant(path));
});
if (swaggerProperties.getExcludePath().isEmpty()) {
swaggerProperties.getExcludePath().addAll(DEFAULT_EXCLUDE_PATH);
}

List<Predicate<String>> excludePath = new ArrayList();
swaggerProperties.getExcludePath().forEach((path) -> {
excludePath.add(PathSelectors.ant(path));
});
return (new Docket(DocumentationType.SWAGGER_2)).host(swaggerProperties.getHost())
.apiInfo(this.apiInfo(swaggerProperties))
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(Predicates.and(Predicates.not(Predicates.or(excludePath)), Predicates.or(basePath)))
.build()
.securitySchemes(Collections.singletonList(this.securitySchema(swaggerProperties)))
.securityContexts(Collections.singletonList(this.securityContext(swaggerProperties)))
.pathMapping("/");
}

private SecurityContext securityContext(SwaggerProperties swaggerProperties) {
return SecurityContext.builder().securityReferences(this.defaultAuth(swaggerProperties))
.forPaths(PathSelectors.regex(swaggerProperties.getAuthorization().getAuthRegex()))
.build();
}

private List<SecurityReference> defaultAuth(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList();
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach((authorizationScope) -> {
authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()));
});
AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()];
return Collections.singletonList(SecurityReference.builder().reference(swaggerProperties.getAuthorization().getName())
.scopes((AuthorizationScope[]) authorizationScopeList.toArray(authorizationScopes))
.build()
);
}

private OAuth securitySchema(SwaggerProperties swaggerProperties) {
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList();
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach((authorizationScope) -> {
authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()));
});
ArrayList<GrantType> grantTypes = new ArrayList();
swaggerProperties.getAuthorization().getTokenUrlList().forEach((tokenUrl) -> {
grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl));
});
return new OAuth(swaggerProperties.getAuthorization().getName(), authorizationScopeList, grantTypes);
}

private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
return (new ApiInfoBuilder()).title(swaggerProperties.getTitle())
.description(swaggerProperties.getDescription())
.license(swaggerProperties.getLicense())
.licenseUrl(swaggerProperties.getLicenseUrl())
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
.contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
.version(swaggerProperties.getVersion())
.build();
}
}

4.4 配置映射类

SwaggerProperties.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("swagger")
public class SwaggerProperties {
private Boolean enabled;
private String basePackage = "";
private List<String> basePath = new ArrayList();
private List<String> excludePath = new ArrayList();
private String title = "";
private String description = "";
private String version = "";
private String license = "";
private String licenseUrl = "";
private String termsOfServiceUrl = "";
private String host = "";
private SwaggerProperties.Contact contact = new SwaggerProperties.Contact();
/** 给所有Api的请求头Header中附加 Authorization 参数,值默认为空 */
private SwaggerProperties.Authorization authorization = new SwaggerProperties.Authorization();


public SwaggerProperties() {
}


public Boolean getEnabled() {
return this.enabled;
}


public String getBasePackage() {
return this.basePackage;
}


public List<String> getBasePath() {
return this.basePath;
}


public List<String> getExcludePath() {
return this.excludePath;
}


public String getTitle() {
return this.title;
}


public String getDescription() {
return this.description;
}


public String getVersion() {
return this.version;
}


public String getLicense() {
return this.license;
}


public String getLicenseUrl() {
return this.licenseUrl;
}


public String getTermsOfServiceUrl() {
return this.termsOfServiceUrl;
}


public String getHost() {
return this.host;
}


public SwaggerProperties.Contact getContact() {
return this.contact;
}


public SwaggerProperties.Authorization getAuthorization() {
return this.authorization;
}


public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}


public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}


public void setBasePath(List<String> basePath) {
this.basePath = basePath;
}


public void setExcludePath(List<String> excludePath) {
this.excludePath = excludePath;
}


public void setTitle(String title) {
this.title = title;
}


public void setDescription(String description) {
this.description = description;
}


public void setVersion(String version) {
this.version = version;
}


public void setLicense(String license) {
this.license = license;
}


public void setLicenseUrl(String licenseUrl) {
this.licenseUrl = licenseUrl;
}


public void setTermsOfServiceUrl(String termsOfServiceUrl) {
this.termsOfServiceUrl = termsOfServiceUrl;
}


public void setHost(String host) {
this.host = host;
}


public void setContact(SwaggerProperties.Contact contact) {
this.contact = contact;
}


public void setAuthorization(SwaggerProperties.Authorization authorization) {
this.authorization = authorization;
}


@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof SwaggerProperties)) {
return false;
} else {
SwaggerProperties other = (SwaggerProperties)o;
if (!other.canEqual(this)) {
return false;
} else {
label167: {
Object this$enabled = this.getEnabled();
Object other$enabled = other.getEnabled();
if (this$enabled == null) {
if (other$enabled == null) {
break label167;
}
} else if (this$enabled.equals(other$enabled)) {
break label167;
}


return false;
}


Object this$basePackage = this.getBasePackage();
Object other$basePackage = other.getBasePackage();
if (this$basePackage == null) {
if (other$basePackage != null) {
return false;
}
} else if (!this$basePackage.equals(other$basePackage)) {
return false;
}


label153: {
Object this$basePath = this.getBasePath();
Object other$basePath = other.getBasePath();
if (this$basePath == null) {
if (other$basePath == null) {
break label153;
}
} else if (this$basePath.equals(other$basePath)) {
break label153;
}


return false;
}


Object this$excludePath = this.getExcludePath();
Object other$excludePath = other.getExcludePath();
if (this$excludePath == null) {
if (other$excludePath != null) {
return false;
}
} else if (!this$excludePath.equals(other$excludePath)) {
return false;
}


label139: {
Object this$title = this.getTitle();
Object other$title = other.getTitle();
if (this$title == null) {
if (other$title == null) {
break label139;
}
} else if (this$title.equals(other$title)) {
break label139;
}


return false;
}


Object this$description = this.getDescription();
Object other$description = other.getDescription();
if (this$description == null) {
if (other$description != null) {
return false;
}
} else if (!this$description.equals(other$description)) {
return false;
}


label125: {
Object this$version = this.getVersion();
Object other$version = other.getVersion();
if (this$version == null) {
if (other$version == null) {
break label125;
}
} else if (this$version.equals(other$version)) {
break label125;
}


return false;
}


label118: {
Object this$license = this.getLicense();
Object other$license = other.getLicense();
if (this$license == null) {
if (other$license == null) {
break label118;
}
} else if (this$license.equals(other$license)) {
break label118;
}


return false;
}


Object this$licenseUrl = this.getLicenseUrl();
Object other$licenseUrl = other.getLicenseUrl();
if (this$licenseUrl == null) {
if (other$licenseUrl != null) {
return false;
}
} else if (!this$licenseUrl.equals(other$licenseUrl)) {
return false;
}


label104: {
Object this$termsOfServiceUrl = this.getTermsOfServiceUrl();
Object other$termsOfServiceUrl = other.getTermsOfServiceUrl();
if (this$termsOfServiceUrl == null) {
if (other$termsOfServiceUrl == null) {
break label104;
}
} else if (this$termsOfServiceUrl.equals(other$termsOfServiceUrl)) {
break label104;
}


return false;
}


label97: {
Object this$host = this.getHost();
Object other$host = other.getHost();
if (this$host == null) {
if (other$host == null) {
break label97;
}
} else if (this$host.equals(other$host)) {
break label97;
}


return false;
}


Object this$contact = this.getContact();
Object other$contact = other.getContact();
if (this$contact == null) {
if (other$contact != null) {
return false;
}
} else if (!this$contact.equals(other$contact)) {
return false;
}


Object this$authorization = this.getAuthorization();
Object other$authorization = other.getAuthorization();
if (this$authorization == null) {
if (other$authorization != null) {
return false;
}
} else if (!this$authorization.equals(other$authorization)) {
return false;
}


return true;
}
}
}


protected boolean canEqual(Object other) {
return other instanceof SwaggerProperties;
}


@Override
public int hashCode() {
boolean PRIME = true;
int result = 1;
Object $enabled = this.getEnabled();
result = result * 59 + ($enabled == null ? 43 : $enabled.hashCode());
Object $basePackage = this.getBasePackage();
result = result * 59 + ($basePackage == null ? 43 : $basePackage.hashCode());
Object $basePath = this.getBasePath();
result = result * 59 + ($basePath == null ? 43 : $basePath.hashCode());
Object $excludePath = this.getExcludePath();
result = result * 59 + ($excludePath == null ? 43 : $excludePath.hashCode());
Object $title = this.getTitle();
result = result * 59 + ($title == null ? 43 : $title.hashCode());
Object $description = this.getDescription();
result = result * 59 + ($description == null ? 43 : $description.hashCode());
Object $version = this.getVersion();
result = result * 59 + ($version == null ? 43 : $version.hashCode());
Object $license = this.getLicense();
result = result * 59 + ($license == null ? 43 : $license.hashCode());
Object $licenseUrl = this.getLicenseUrl();
result = result * 59 + ($licenseUrl == null ? 43 : $licenseUrl.hashCode());
Object $termsOfServiceUrl = this.getTermsOfServiceUrl();
result = result * 59 + ($termsOfServiceUrl == null ? 43 : $termsOfServiceUrl.hashCode());
Object $host = this.getHost();
result = result * 59 + ($host == null ? 43 : $host.hashCode());
Object $contact = this.getContact();
result = result * 59 + ($contact == null ? 43 : $contact.hashCode());
Object $authorization = this.getAuthorization();
result = result * 59 + ($authorization == null ? 43 : $authorization.hashCode());
return result;
}


@Override
public String toString() {
return "SwaggerProperties(enabled=" + this.getEnabled() + ", basePackage=" + this.getBasePackage() + ", basePath=" + this.getBasePath() + ", excludePath=" + this.getExcludePath() + ", title=" + this.getTitle() + ", description=" + this.getDescription() + ", version=" + this.getVersion() + ", license=" + this.getLicense() + ", licenseUrl=" + this.getLicenseUrl() + ", termsOfServiceUrl=" + this.getTermsOfServiceUrl() + ", host=" + this.getHost() + ", contact=" + this.getContact() + ", authorization=" + this.getAuthorization() + ")";
}


public static class AuthorizationScope {
private String scope = "";
private String description = "";


public String getScope() {
return this.scope;
}


public String getDescription() {
return this.description;
}


public void setScope(String scope) {
this.scope = scope;
}


public void setDescription(String description) {
this.description = description;
}


@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof SwaggerProperties.AuthorizationScope)) {
return false;
} else {
SwaggerProperties.AuthorizationScope other = (SwaggerProperties.AuthorizationScope)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$scope = this.getScope();
Object other$scope = other.getScope();
if (this$scope == null) {
if (other$scope != null) {
return false;
}
} else if (!this$scope.equals(other$scope)) {
return false;
}


Object this$description = this.getDescription();
Object other$description = other.getDescription();
if (this$description == null) {
if (other$description != null) {
return false;
}
} else if (!this$description.equals(other$description)) {
return false;
}


return true;
}
}
}


protected boolean canEqual(Object other) {
return other instanceof SwaggerProperties.AuthorizationScope;
}


@Override
public int hashCode() {
boolean PRIME = true;
int result = 1;
Object $scope = this.getScope();
result = result * 59 + ($scope == null ? 43 : $scope.hashCode());
Object $description = this.getDescription();
result = result * 59 + ($description == null ? 43 : $description.hashCode());
return result;
}


@Override
public String toString() {
return "SwaggerProperties.AuthorizationScope(scope=" + this.getScope() + ", description=" + this.getDescription() + ")";
}


public AuthorizationScope() {
}
}


public static class Authorization {
private String name = "";
private String authRegex = "^.*$";
private List<SwaggerProperties.AuthorizationScope> authorizationScopeList = new ArrayList();
private List<String> tokenUrlList = new ArrayList();


public String getName() {
return this.name;
}


public String getAuthRegex() {
return this.authRegex;
}


public List<SwaggerProperties.AuthorizationScope> getAuthorizationScopeList() {
return this.authorizationScopeList;
}


public List<String> getTokenUrlList() {
return this.tokenUrlList;
}


public void setName(String name) {
this.name = name;
}


public void setAuthRegex(String authRegex) {
this.authRegex = authRegex;
}


public void setAuthorizationScopeList(List<SwaggerProperties.AuthorizationScope> authorizationScopeList) {
this.authorizationScopeList = authorizationScopeList;
}


public void setTokenUrlList(List<String> tokenUrlList) {
this.tokenUrlList = tokenUrlList;
}


@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof SwaggerProperties.Authorization)) {
return false;
} else {
SwaggerProperties.Authorization other = (SwaggerProperties.Authorization)o;
if (!other.canEqual(this)) {
return false;
} else {
label59: {
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name == null) {
break label59;
}
} else if (this$name.equals(other$name)) {
break label59;
}


return false;
}


Object this$authRegex = this.getAuthRegex();
Object other$authRegex = other.getAuthRegex();
if (this$authRegex == null) {
if (other$authRegex != null) {
return false;
}
} else if (!this$authRegex.equals(other$authRegex)) {
return false;
}


Object this$authorizationScopeList = this.getAuthorizationScopeList();
Object other$authorizationScopeList = other.getAuthorizationScopeList();
if (this$authorizationScopeList == null) {
if (other$authorizationScopeList != null) {
return false;
}
} else if (!this$authorizationScopeList.equals(other$authorizationScopeList)) {
return false;
}


Object this$tokenUrlList = this.getTokenUrlList();
Object other$tokenUrlList = other.getTokenUrlList();
if (this$tokenUrlList == null) {
if (other$tokenUrlList != null) {
return false;
}
} else if (!this$tokenUrlList.equals(other$tokenUrlList)) {
return false;
}


return true;
}
}
}


protected boolean canEqual(Object other) {
return other instanceof SwaggerProperties.Authorization;
}


@Override
public int hashCode() {
boolean PRIME = true;
int result = 1;
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $authRegex = this.getAuthRegex();
result = result * 59 + ($authRegex == null ? 43 : $authRegex.hashCode());
Object $authorizationScopeList = this.getAuthorizationScopeList();
result = result * 59 + ($authorizationScopeList == null ? 43 : $authorizationScopeList.hashCode());
Object $tokenUrlList = this.getTokenUrlList();
result = result * 59 + ($tokenUrlList == null ? 43 : $tokenUrlList.hashCode());
return result;
}


@Override
public String toString() {
return "SwaggerProperties.Authorization(name=" + this.getName() + ", authRegex=" + this.getAuthRegex() + ", authorizationScopeList=" + this.getAuthorizationScopeList() + ", tokenUrlList=" + this.getTokenUrlList() + ")";
}


public Authorization() {
}
}


public static class Contact {
private String name = "";
private String url = "";
private String email = "";


public String getName() {
return this.name;
}


public String getUrl() {
return this.url;
}


public String getEmail() {
return this.email;
}


public void setName(String name) {
this.name = name;
}


public void setUrl(String url) {
this.url = url;
}


public void setEmail(String email) {
this.email = email;
}


@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof SwaggerProperties.Contact)) {
return false;
} else {
SwaggerProperties.Contact other = (SwaggerProperties.Contact)o;
if (!other.canEqual(this)) {
return false;
} else {
label47: {
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name == null) {
break label47;
}
} else if (this$name.equals(other$name)) {
break label47;
}


return false;
}


Object this$url = this.getUrl();
Object other$url = other.getUrl();
if (this$url == null) {
if (other$url != null) {
return false;
}
} else if (!this$url.equals(other$url)) {
return false;
}


Object this$email = this.getEmail();
Object other$email = other.getEmail();
if (this$email == null) {
if (other$email != null) {
return false;
}
} else if (!this$email.equals(other$email)) {
return false;
}


return true;
}
}
}


protected boolean canEqual(Object other) {
return other instanceof SwaggerProperties.Contact;
}


@Override
public int hashCode() {
boolean PRIME = true;
int result = 1;
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $url = this.getUrl();
result = result * 59 + ($url == null ? 43 : $url.hashCode());
Object $email = this.getEmail();
result = result * 59 + ($email == null ? 43 : $email.hashCode());
return result;
}


@Override
public String toString() {
return "SwaggerProperties.Contact(name=" + this.getName() + ", url=" + this.getUrl() + ", email=" + this.getEmail() + ")";
}


public Contact() {
}
}
}

4.5 配置文件

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server:
port: 8088
#解决Springboot启动报错:Failed to start bean 'documentationPluginsBootstrapper'
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
#swagger配置
swagger:
enabled: true
title: 测试Swagger
description: 这是一次对自动配置对象的注入和测试swagger配置类的自动配置
version: 1.0
host: localhost
termsOfServiceUrl: http://${swagger.host}:${server.port}/doc.html
contact:
name: 姜源
url: http://www.xxx.com
email: jerry@xxx.com

4.6 配置Spring工厂

resources/META-INF/spring.factories

1
2
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jerry.swagger.config.SwaggerAutoConfiguration

4.7 测试接口+启动类

测试接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
@Api(tags = "测试接口")
public class TestController {

@ApiOperation(value = "测试", notes = "测试")
@PostMapping("/api")
public String test() {
return "OK";
}
}

启动类:

1
2
3
4
5
6
7
8
9
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}

4.8 使用方式

  • 单服务中直接配置使用;
  • 打 jar 包放私服上供其他服务直接引用,启动自带swagger。

01-Swagger不同版本集成与聚合
https://janycode.github.io/2022/04/06/15_分布式/00_Api文档/01-Swagger不同版本集成与聚合/
作者
Jerry(姜源)
发布于
2022年4月6日
许可协议