swagger 是一个功能强大的api框架,不仅提供了在线文档的查阅,而且还提供了在线文档的测试
1、引入依赖
<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>
2、创建配置类
我们创建一个swagger2Config文件
@Configuration
@EnableSwagger2 // 开启swagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 指定扫描的包
.apis(RequestHandlerSelectors.basePackage("com.wzp.ljy"))
.paths(PathSelectors.any())
.build();
}
// 配置一些基本的信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swaggerApi文档")
.description("http://www.wzp.com/wzp")
.termsOfServiceUrl("http://www.wzp.com/wzp")
.version("1.0")
.build();
}
}
3、一个简单的例子
@RestController
@Api(tags = "公共接口管理")
public class commonController {
/**
* 获取系统时间
* @return
*/
@GetMapping("/getTime")
@ApiOperation("获取系统时间")
public Map Time(){
Long sysTime = System.currentTimeMillis();
Map<String,Long> map = new HashMap<>();
map.put("sysTime",sysTime);
return map;
}
启动工程,访问:http://localhost:8081/swagger-ui.html 可以看到如下界面,则说明swagger生效成功
4、附上一份swagger相关注解的解释
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiParamImplicitL:一个请求参数
@ApiParamsImplicit 多个请求参数
5、结语
按照惯例写个结语,嗯...看了很多大佬的教程,结合我自己的总结了一波,当然了,不足之处请多包涵,也请多指教...如有雷同,也请多包涵......