1. 新建项目




2. 导入相关依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

3. 编写一个示例程序

4. 编写 Swagger配置

5. 测试运行
http://localhost:8080/swagger-ui.html#/
6. 配置 Swagger
@Configuration
@EnableSwagger2 // 开启 Swagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
public ApiInfo apiInfo(){
Contact contact = new Contact("cxx", "#", "cxx404@outlook.com");
return new ApiInfo(
"Swagger API 文档",
"略",
"1.0",
"#",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}

本文介绍如何使用Springfox和Swagger2为Spring Boot项目快速生成API文档,包括项目搭建、依赖导入、示例程序编写、Swagger配置及测试运行。
2771

被折叠的 条评论
为什么被折叠?



