swagger:
swagger是一个规范和完整的框架,可以生成,描述,调用和可视化RESTful 风格的 Web 服务。
springboot整合swagger
添加依赖
有两种模式,一个是swagger-ui.html,一个是doc.html(按自己要求所取)
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--swagger-ui.html模式 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--doc.html模式 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.2</version>
</dependency>创建swagger配置类:
package com.yan.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration // 告诉springboot需要加载这个配置类
@EnableSwagger2 // 启用swagger
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 通过select返回一个实例,用来控制哪些接口暴露给swagger来展示。
.select()
// 对所有api进行监控
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
// 创建api的基本信息,(这些信息会展示在文档中,比如title描述之类的)
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("spring-web项目")
.description("对项目的描述").build();
}
}
验证:
项目启动之后,可以在访问localhost:8080/swagger-ui.html或者localhost:8080/doc.html来访问了。(根据自己依赖导入来选择)
一些注解:
Controller层
定义接口名称:@Api(tags="xxx")
定义方法名称:@ApiOperation(value="xxx")
定义param参数的各个属性:@ApiImplicitParam(name = "xx", value = "xx", paramType = "xx")
如: @ApiImplicitParam(name = "pid", value = "产品id", paramType = "String")
实体
定义对象名称:@ApiModel("xxx")
定义参数名称:@ApiModelProperty(value = "xxx")版权声明:本文为qq_45443879原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。