swagger是什么
- 是一款让你更好的书写API文档的规范且完整框架。提供描述、生产、消费和可视化RESTful Web Service。
- 是由庞大工具集合支撑的形式化规范。这个集合涵盖了从终端用户接口、底层代码库到商业API管理的方方面面
实现步骤
- 在pom.xml文件中添加swagger相关依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
- 创建swagger的配置类
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.donwait.controller"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试接口文档名称")
.description("提供接口服务描述")
.version("1.0")
.build();
}
}
- 项目结构
-
默认访问路径:http://localhost:8080/swagger-ui.html#/
-
修改默认访问路径:在application.yml中添加,访问路径为: http://localhost:8080/test/swagger-ui.html#/
server: servlet: context-path: /test
-
Swagger常用API
地址:[详细描述](https://blog.csdn.net/wyb880501/article/details/79576784)
- @Api()用于类;
表示标识这个类是swagger的资源 - @Api()用于类;
表示标识这个类是swagger的资源 - @ApiOperation()用于方法;
表示一个http请求的操作 - @ApiParam()用于方法,参数,字段说明;
表示对参数的添加元数据(说明或是否必填等) - @ApiModel()用于类
表示对类进行说明,用于参数用实体类接收 - @ApiModelProperty()用于方法,字段
表示对model属性的说明或者数据操作更改 - @ApiIgnore()用于类,方法,方法参数
表示这个方法或者类被忽略 - @ApiImplicitParam() 用于方法
表示单独的请求参数 - @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
这些注解还需要自己亲自尝试才能理解其用途,希望大家多动手操作操作~~