Springboot整合swagger
一、前提
已经搭建好springboot环境,并正确运行!!! 搭建Springboot框架:王林博客_Springboot框架搭建
二、导入依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
三、配置application.yml 文件
swagger:
enabled:
application-name: ${spring.application.name}
application-version: 1.0
application-description: 项目接口文档
try-host: http://localhost:${server.port}
logging:
file:
path: ./logs/frame/
四、新建配置类Swagger2Config
@Configuration
@EnableOpenApi
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
{"enable"},havingValue = "true")
public class Swagger2Config {
@Bean(value = "defaultApi2")
public Docket defaultApi2(Environment environment) {
String groupName = "2.X版本";
Profiles profiles = Profiles.of("dev", "test");
boolean flag = environment.acceptsProfiles(profiles);
Docket docket = new Docket(DocumentationType.OAS_30)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cqie.cn.frame.controller"))
.paths(PathSelectors.any())
.build()
.enable(flag);
return docket;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("项目文档Api")
.description("swagger2 API 文档")
.termsOfServiceUrl("")
.contact(new Contact("王林", "/doc.html", "2741933961@qq.com"))
.version("1.0")
.build();
}
}
四、测试swagger
访问地址:http://localhost:8085/doc.html
到此结束 完接★,°:.☆( ̄▽ ̄)/$:.°★ 。
|