swagger2的作用:使用代码生成文档api给前端对接
引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<!-- 官方的ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<!-- 第三方的ui-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.0</version>
</dependency>
注入swagger的配置bean
@Configuration
@EnableSwagger2
public class Swagger2 {
// localhost:8080为项目的地址和ip
// http://localhost:8080/swagger-ui.html 原路径
// http://localhost:8080/doc.html 第三方路径
// 配置swagger2核心配置 docket
@Bean
public Docket createRestApi() {
// 指定api类型为swagger2.0版本
return new Docket(DocumentationType.SWAGGER_2)
// 用于定义api文档汇总信息(打开的swagger网页)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors
.basePackage("com.springboot.demo.controller")) // 指定controller包
.paths(PathSelectors.any()) // 所有controller
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger文档demo接口api") // 文档页标题
.contact(new Contact("一个不知名的网站",
"www.一个不知名的网站地址.com",
"xxxxx@qq.com")) // 联系人信息
.description("一个不知名的网站的api文档") // 详细信息
.version("1.0.0") // 文档版本号
.termsOfServiceUrl("www.一个不知名的网站地址.com") // 网站地址
.build();
}
}
查看文档
使用方法
@ApiIgnore:在文档中被忽略
@Api:解释controller
@ApiOperation:解释方法
@ApiModel @ApiModelProperty:解释入参