SpringBoot同时集成swagger与knife4j2种API文档
程序员文章站
2022-07-12 21:18:00
...
1.先来看看swagger与knife4j的外观
2.接下来讲如何同时配置swagger与knife4j
2.1配置pom依赖
<!-- knife4j版接口文档 访问/doc.html -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<!--在引用时请在maven*仓库搜索最新版本号-->
<version>2.0.4</version>
</dependency>
<!-- swagger 版接口文档 访问/swagger-ui.html -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.2 配置spring config配置类
新建1个java类
ApiDocConfig.java
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class ApiDocConfig {
private final ServerProperties serverProperties;
private final SysInfoProperties sysInfoProperties;
public Knife4jConfig(ServerProperties serverProperties, SysInfoProperties sysInfoProperties) {
this.serverProperties = serverProperties;
this.sysInfoProperties = sysInfoProperties;
}
private ApiInfo apiInfo() {
String serviceUrl = "http://localhost:" + serverProperties.getPort() +
serverProperties.getServlet().getContextPath();
return new ApiInfoBuilder()
.title("xxxx服务后台接口文档")
.description("xxxx服务")
.termsOfServiceUrl(serviceUrl)
.version(sysInfoProperties.getVersion())
.contact(new Contact(sysInfoProperties.getPic(), null, "aaa@qq.com"))
.build();
}
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//分组名称
.groupName("1.X版本接口")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.xxxxx.contentstatistics.web"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
3.相关的SysInfoProperties.java
@Data
@Component
// 注册为组件
@EnableConfigurationProperties
// 启用配置自动注入功能
@ConfigurationProperties(prefix = "project")
//指定类对应的配置项前缀
@ApiModel(description = "系统描述")
public class SysInfoProperties {
@ApiModelProperty(value = "完整应用名")
private String application;
@ApiModelProperty(value = "应用名")
private String name;
@ApiModelProperty(value = "应用中文名")
private String chineseName;
@ApiModelProperty(value = "版本")
private String version;
@ApiModelProperty(value = "开发")
private String pic;
@ApiModelProperty(value = "框架")
private String framework;
}
上一篇: Browserify,初学
下一篇: Java线程学习笔记(二):线程的创建