【问题标题】:How can I use swagger to export java project api?如何使用 swagger 导出 java 项目 api?
【发布时间】:2017-07-10 05:31:36
【问题描述】:
我创建了一个springboot+mybatis+swagger项目,我想用swagger来导出项目api,我的代码里确实写了swagger注解。
如何将 api 导出为 html 或 doc 或 chm?
【问题讨论】:
标签:
java
api
spring-boot
swagger
【解决方案1】:
要引入 Swagger,我们需要在 Maven POM 中声明以下依赖项
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
在应用程序中配置 Swagger 2
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select() .apis(RequestHandlerSelectors.basePackage("com.springframework.example"))
.paths("/")
.build();
}
}
此时,您应该可以通过启动应用程序并将浏览器指向http://localhost:8080/v2/api-docs来测试配置
将浏览器指向http://localhost:8080/swagger-ui.html,您将看到由 Swagger UI 呈现的生成文档。
Reference Document