springboot集成swagger-ui
Swagger是一个Restful风格接口的文档在线自动生成和测试的框架
关于springboot的搭建可以参考我的第一篇文章.这里直接进入正题.
1.poi.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
2.configuration文件夹下创建Swagger
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger", name = "enabled", havingValue = "true", matchIfMissing = true)
public class Swagger {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
@Bean
public Docket createRestApi() {//登录验证暂时用不到
// List<Parameter> pars = new ArrayList<>();
// ParameterBuilder tokenPar = new ParameterBuilder();
// tokenPar.name("token")
// .description("用户token")
// .modelRef(new ModelRef("string"))
// .parameterType("header")
// .required(false)
// .build();
// pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//修正Byte转string的Bug
.directModelSubstitute(Byte.class, Integer.class)
// .globalOperationParameters(pars)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.test.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("Swagger API 接口文档")
.contact("Yaoyao.Mei")
.version("1.0")
.build();
}
}
3.application.yml
## swagger配置
swagger:
enabled: true
备注:
@ConditionalOnProperty(prefix = "swagger", name = "enabled", havingValue = "true", matchIfMissing = true)
作用:在application.yml 可以设置是否开启
关于Swagger使用的注解及其说明,请参考https://blog.****.net/sanyaoxu_2/article/details/80555328