使用 Swagger 时如果碰见版本更新,只需要更改Swagger的描述文件即可。但是在频繁地更新项目版本时很多开发人员认为即使修改描述文件(yml 或 json)也有一定的工作负担,久而久之就直接修改代码,而不去修改描述文件了,这样基于描述文件生成接口文档也就失去了意义。 由于Spring的流行,Marty Pitt 编写了一个基于Spring 的组件swagger-springmvc。Spring-fox 就是根据这个组件发展而来的全新项目。Spring-fox 是根据代码生成接口文档,所以正常的进行更新项目版本,修改代码即可,而不需要跟随修改描述文件。Spring-fox 利用自身 AOP 特性,把 Swagger 集成进来,底层还是Swagger。但是使用起来确方便很多。所以在实际开发中,都是直接使用 spring-fox。
官网地址:
https://github.com/springfox/springfox
- spring-fox依赖
<!--spring-fox依赖-->
<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>
- spring-fox与springBoot整合时依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
@SpringBootApplication
@EnableSwagger2 //使用swagger注解
@EnableWebMvc
public class SwaggerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerDemoApplication.class, args);
}
}
//访问地址 http://localhost:8080/swagger-ui/index.html