废话不说 直接分析
-
什么是SwaggerUI?参考 https://www.cnblogs.com/JoiT/p/6378086.html
Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因:Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API。
Swagger 可以生成客户端SDK代码用于各种不同的平台上的实现。
Swagger 文件可以在许多不同的平台上从代码注释中自动生成。
Swagger 有一个强大的社区,里面有许多强悍的贡献者
2、搭建一个SpringBoot 案例 集成SwaggerUI 导入对应Maven pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox </groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>
3、编写Swagger 加载配置类
package com.api.swagger;
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;
/**
*
* @ClassName: Swagger2Config
* @Description:TODO(swagger 配置)
* @author: drj
* @date: 2019年1月19日 下午8:10:52
*
* @Copyright: 2019
*
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.api")).paths(PathSelectors.any())
.build();
}//PathSelectors.regex("/v1/")
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("dairuijie Restful API").description("代瑞杰 API")
.version("1.0").build();
}
}
4、启动项目,浏览器输入 http://127.0.0.1:8888/swagger-ui.html# 自己的项目ip+port 如图:
5、git 项目地址 https://github.com/dairuijie/api.git 有具体注解 使用demo