Swagger 和 Springfox-Swagger 的关系

Swagger(Springfox-Swagger) 有一段时间, 但是一直搞懂它们之间的关系,最终还是从官方文档上了解到:

原文:

What is the relationship between swagger-ui and springfox-swagger-ui?

  • Swagger Spec is a specification.

  • Swagger Api - an implementation of that specification that supports jax-rs, restlet, jersey etc.

  • Springfox libraries in general - another implementation of the specification focused on the spring based ecosystem.

  • Swagger.js and Swagger-ui - are client libraries in javascript that can consume swagger specification.

  • springfox-swagger-ui - the one that you’re referring to, is just packaging swagger-ui in a convenient way so that spring services can serve it up.

翻译下来就是:

swagger-ui 和 springfox-swagger-ui 的关系是?

  • Swagger Spec 是一个规范。

  • Swagger ApiSwagger Spec 规范 的一个实现,它支持 jax-rs, restlet, jersey 等等。

  • Springfox librariesSwagger Spec 规范 的另一个实现,专注于 spring 生态系统

  • Swagger.js and Swagger-ui 是 javascript 的客户端库,能消费该规范。

  • springfox-swagger-ui 仅仅是以一种方便的方式封装了 swagger-ui ,使得 Spring 服务可以提供服务。

好了,总的来说就是:

Swagger 是一种规范。

springfox-swagger 是基于 Spring 生态系统的该规范的实现。

springfox-swagger-ui 是对 swagger-ui 的封装,使得其可以使用 Spring 的服务。

参考:

### Springfox-Swagger-UI Springfox-Boot-Starter 的区别 #### 1. **Springfox-Swagger-UI** `springfox-swagger-ui` 是一个用于提供 Swagger UI 页面支持的模块。它主要负责渲染 API 文档页面,使开发者能够通过浏览器直观地查看测试接口功能。 - 它是一个独立的依赖项,通常与其他 `springfox` 模块(如 `springfox-swagger2` 或其他版本)一起使用。 - 主要职责是加载并展示 Swagger UI 界面。 - 需要在项目中手动配置路径映射以及相关资源文件的位置[^1]。 ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> ``` 此模块适合于需要高度自定义Swagger界面或者不希望自动绑定到特定框架中的场景。 --- #### 2. **Springfox-Boot-Starter** `springfox-boot-starter` 则是一个专门为 Spring Boot 设计的一站式解决方案。它的目标是简化集成过程,并减少开发者的手动配置工作量。 - 自动化程度更高,内置了许多默认设置,减少了繁琐的 Bean 注册路径映射操作。 - 提供了更现代化的支持方式,兼容最新的 Spring Boot 版本及其特性。 - 更加推荐给那些基于现代 Spring Boot 架构构建的应用程序使用[^4]。 ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>${swagger.version}</version> </dependency> ``` 对于大多数新项目而言,直接引入该 Starter 可以快速实现 API 文档的功能而无需额外调整太多参数。 --- #### 3. **两者的主要差异** | 属性 | Springfox-Swagger-UI | Springfox-Boot-Starter | |---------------------|---------------------------------------------|-------------------------------------------| | 功能范围 | 负责显示 Swagger UI | 综合管理 Swagger 文档生成与 UI 显示 | | 手动配置需求 | 较高 | 较低 | | 默认自动化能力 | 基础 | 强大 | | 推荐使用的场景 | 对旧版 Spring Boot 或特殊定制有较高要求时 | 新建项目或标准 Spring Boot 应用 | 当选择具体库的时候可以根据团队的技术栈成熟度个人偏好来决定采用哪一个方案[^2]。 --- ### 示例代码对比 以下是两种不同情况下如何启用 Swagger 的简单例子: #### 使用 `springfox-swagger-ui` ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } ``` #### 使用 `springfox-boot-starter` 只需添加依赖即可完成大部分基础配置: ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 在这种模式下,几乎不需要编写任何额外 Java Config 来启动服务[^3]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值