官网:https://swagger.io/
官方文档:https://swagger.io/docs/
1.使用IDEA创建项目
2.更改项目名和组织名
3.选择版本和依赖
4.选择项目路径
5.导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
6.创建一个Controller
@RestController
public class Hello {
@GetMapping("hello")
public String hello(){
return "hello swagger";
}
}
7.添加swagger配置类
@Configuration //表示这是一个配置类
@EnableSwagger2 //开启Swagger使用
public class SwaggerConfig {
//swagger2的配置类,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createTestApi() {// 创建API基本信息
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
//扫描所有的包 可以扫描指定的包 .apis(RequestHandlerSelectors.basePackage("具体controller所在的包"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){// 创建API的基本信息,这些信息会在Swagger UI中进行显示
return new ApiInfoBuilder()
.title("Swagger接口文档")//标题
.description("Swagger-接口文档")// API描述
.version("1.0.0")//接口的版本
.build();
}
}
8.启动项目
访问swaggerUI界面http://localhost:8080/swagger-ui.html#/
成功
项目源码地址:https://github.com/xiaokun-wsk/boot-swagger
9.swagger中注解详细使用参考文章
注解使用