一:引入依赖
<!--引入knife4j依赖 开始-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<!--引入knife4j依赖 结束-->
二:编写配置类
/**
* @Description:
* @Author: sgl
* @Date: 2020/12/8 0008 上午 11:25
*/
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.sgl.retry.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger-bootstrap-ui RESTful APIs")
.description("swagger-bootstrap-ui")
.termsOfServiceUrl("http://localhost:5050/")
.contact("93667454@qq.com")
.version("1.0")
.build();
}
}
注意配置类里面的
RequestHandlerSelectors.basePackage("com.sgl.retry.controller") 要扫描你的项目的controller目录,不然swagger无法正常显示
三:使用注解
类注解
@Api(tags = {"重试类"}, description = "重试机制类")
方法注解
@ApiOperation(value = "进行重试")
四:查看效果
启动项目
访问
http://127.0.0.1:1994/doc.html
项目地址 + 端口号 + /doc.html
具体参考官网文档:https://xiaoym.gitee.io/knife4j/documentation/description.html