一、搭建项目
- 打开Idea,点击【File】-【New】-【Project...】-【Spring Initializr】-点击【Next】-【Next】
- 在添加依赖的页面,点击【Web】-勾选【Web】-【Next】-【Finish】。项目即创建成功。(中途可自行修改项目名称等)
二、添加依赖
1.打开pom.xml文件,添加swagger的两个依赖。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
三、Swagger配置类
1.在项目目录下创建包“config”,并添加配置类SwaggerConfig.java,内容如下:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket applicationApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("fakerswe")
.select() // 选择哪些路径和api会生成document
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any()) // 对所有路径进行监控
.build()
.apiInfo(applicationInfo());//用来创建该Api的基本信息(这些基本信息会展现在文档页面中)
}
private ApiInfo applicationInfo() {
ApiInfo apiInfo = new ApiInfo("接口管理",//大标题
"api接口可视化管理" ,//小标题
"0.1",//版本
"",
new Contact("叶湘伦", "", ""),// 作者
"",//链接显示文字
""//网站链接
);
return apiInfo;
}
}
2.扫描启动类-DemoApplication.java
@SpringBootApplication
@ComponentScan("com.*")//此处需进行扫描
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}