今天后端的朋友用了swagger管理接口,非常好用,記錄一下。
第一步:添加依賴
這個(gè)可以在任意一個(gè)pom.xml里面布置,我這里放在Controller的pom.xml里面
<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>
第二步:把一個(gè)WebMvcConfigurer的實(shí)現(xiàn)類和啟動(dòng)項(xiàng)放在一起

image.png
在swagger2類中設(shè)置參數(shù)
@Configuration
@EnableSwagger2
public class Swagger2 implements WebMvcConfigurer {
// 接口版本號(hào)
private final String version = "1.0";
// 接口大標(biāo)題
private final String title = "xxx接口";
// 具體的描述
private final String description = "公共數(shù)據(jù)服務(wù)接口文檔";
// basePackage
private final String basePackage = "com.snnu.mbts.controller";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.version(version)
.build();
}
}
第三步:在Controller類中設(shè)置接口信息
比如接口名稱、接口信息,接口參數(shù)
@ApiOperation(value = "用戶登陸請(qǐng)求", notes = "注意事項(xiàng)")
@ApiImplicitParams({
@ApiImplicitParam(name = "loginName", value = "用戶名", required = true, paramType = "query", example = "張三"),
@ApiImplicitParam(name = "password", value = "密碼", required = true, paramType = "query", example = "111111")
})