首先,依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.21</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.21</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.7.3</version> </dependency> <dependency> <groupId>com.github.caspar-chen</groupId> <artifactId>swagger-ui-layer</artifactId> <version>1.1.3</version> </dependency>
然后,配置
@EnableSwagger2
@Configuration
public class Swagger2Config {
@Bean
public Docket createRestApi() {
//添加head参数start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("")
.select()
.apis(RequestHandlerSelectors.basePackage("xxx.xxx.xxx.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("AGUAI-测试API接口平台")
.description("业务系统模块,结合token使用")
.version("2.6.0")
.license("The Apache License")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
.build())
.globalOperationParameters(pars);
}
}
根据项目可以酌情修改,我们的系统需要token,所以在header参数中有token,不需要的可以直接删。
最后,成品
就是这样了,直接访问:端口号/docs.html
在controller加入注解:@Api(tags = "视频管理类"),接口上加入注解:@ApiOperation("视频列表"),那么接口平台就可以更加清晰定位到接口。
还有一点,可以直接在接口平台中测试,操作如下
但是有一点不好,选择了别的接口后再回到本接口,就要重新写入参数。
结束,告辞!