简介:
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
也就是说只要我们配置好swagger,swagger就会自动帮我们生成api文档,同时我们也可以对接口进行测试,省去了后端开发人员对接口的编写,方便了接口的测试。
添加依赖:
<!--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>
编写配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("题目")
.description("接口文档描述")
.version("1.0")
.contact(new Contact("zyw", "http://zyw.com", "1123@qq.com"))
.build();
}
}
为了增加api文档的可读性,可以在controller上加注解,从而给文档加描述声明:
@Api(description = "账户管理") //加在类上
@ApiOperation("查询所有账户") //加在方法上
@ApiParam(value = "账户id",required = true) //加在属性上
例如:
启动spring boot项目 访问:
http://127.0.0.1:8080/swagger-ui.html
可以直接在这里测试,省去了api开发文档的编写,也方便了接口测试。
帮助到您请点赞关注收藏谢谢!!!