Swagger是自动生成 REST APIs文档的工具之一。Swagger支持jax-rs, restlet, jersey。springfox-swagger是Spring生态的Swagger解决方案。
整合步骤:
-
创建Spring Boot项目(略)
-
导入springfox-swagger依赖包, 在pox.xml中加入:
<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>
- 新增配置类SwaggerConfig,内容如下:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).//根据文档类型初始化
apiInfo(swaggerApiInfo()) //文档头信息
.select()
.apis(RequestHandlerSelectors.basePackage("com.osxm.splm")) //过滤条件
.paths(PathSelectors.any()) //过滤路径
.build();
}
/**
* 自定义API文档基本信息实体
*
* @return
*/
private ApiInfo swaggerApiInfo() {
Contact contact = new Contact("XM", "http://www.osxm.com", "[email protected]"); // 联系人,在UI界面会显示
return new ApiInfoBuilder().contact(contact)
.title("Swagger2构建RESTful API文档") // 文档标题
.description("SpringBoot构建成RESTful API文档") // 文档描述
.version("1.0.0").build(); // 文档版本
}
}
- 启动Spring Boot应用,访问以下路径:
http://localhost:8080/swagger-ui.html