当前位置: 首页 > news >正文

Springfox + Swagger 的完整配置及同类框架对比的详细说明

以下是 Springfox + Swagger 的完整配置及同类框架对比的详细说明:
在这里插入图片描述
在这里插入图片描述


一、Springfox + Swagger 配置详解

1. 添加依赖

pom.xml 中添加以下依赖:

<!-- Springfox Swagger 2 -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>3.0.0</version> <!-- 注意版本兼容性 -->
</dependency>
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>3.0.0</version>
</dependency>

2. 配置类(SwaggerConfig.java)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()// 指定Controller包路径.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))// 指定路径(可选,PathSelectors.any() 表示所有路径).paths(PathSelectors.ant("/api/**")).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("API文档").description("系统接口文档").version("1.0.0").contact(new Contact("开发者", "https://example.com", "dev@example.com")).license("Apache 2.0").licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html").build();}
}

3. 自定义访问地址

通过修改 application.propertiesapplication.yml 自定义 Swagger UI 的访问路径:

# application.properties
springfox.documentation.swagger.v2.path=/custom-api-docs
springfox.documentation.swagger-ui.path=/custom-swagger-ui

访问地址变为:
http://localhost:8080/custom-swagger-ui


4. Controller 示例
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api")
@Api(tags = "示例接口") // 接口分组标签
public class ExampleController {@GetMapping("/hello")@ApiOperation("Hello接口")public String sayHello() {return "Hello, Swagger!";}
}

二、同类框架对比

1. Springdoc OpenAPI
  • 特点
    • 支持 OpenAPI 3.0 标准。
    • 自动扫描,无需复杂配置。
    • 支持 Swagger UI 和 ReDoc。
  • 配置示例
    <!-- pom.xml -->
    <dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-ui</artifactId><version>1.6.12</version>
    </dependency>
    
  # application.propertiesspringdoc.api-docs.path=/api-docsspringdoc.swagger-ui.path=/swagger-ui
  • 访问地址http://localhost:8080/swagger-ui

2. Swagger UI (官方版)
  • 特点
    • 官方维护的 UI 组件。
    • 需手动配置 OpenAPI 定义文件。
    • 灵活适配多种后端技术。
  • 配置示例
    <!-- pom.xml -->
    <dependency><groupId>io.swagger.core.v3</groupId><artifactId>swagger-ui</artifactId><version>2.2.10</version>
    </dependency>
    
  @SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication app = new SpringApplication(Application.class);// 配置 Swagger UI 路径app.setDefaultProperties(Collections.singletonMap("spring.resources.static-locations", "classpath:/META-INF/resources/"));app.run(args);}}

3. RAML
  • 特点
    • 基于 RAML(RESTful API Modeling Language)描述接口。
    • 支持自动生成文档和客户端代码。
    • 适合需要详细接口规范的团队。
  • 工具:使用 RAML ResolverRAML Console 生成文档。

4. Apigee
  • 特点
    • 企业级 API 管理平台。
    • 提供完整的 API 生命周期管理(发布、监控、安全等)。
    • 支持多语言文档生成。
  • 适用场景:大型分布式系统、微服务架构。

三、功能对比表格

框架支持协议配置复杂度适用场景核心功能访问方式
SpringfoxSwagger 2.0中等Spring Boot 项目、简单配置自动扫描、注解配置、支持分组http://localhost:8080/swagger-ui.html
Springdoc OpenAPIOpenAPI 3.0现代 Spring Boot 项目、自动扫描支持 OpenAPI 3、ReDoc、Swagger UIhttp://localhost:8080/swagger-ui
Swagger UIOpenAPI 2/3需手动配置 OpenAPI 定义文件灵活适配多种后端技术自定义路径
RAMLRAML需详细接口规范的团队自动生成文档和客户端代码需第三方工具支持
Apigee多种协议企业级 API 管理、复杂系统生命周期管理、监控、安全策略云端或私有部署

四、选择建议

  • 简单快速集成:选择 Springdoc OpenAPI(支持 OpenAPI 3,配置简单)。
  • Spring Boot 项目:优先 SpringfoxSpringdoc
  • 手动控制文档:使用 Swagger UIRAML
  • 企业级需求:选择 Apigee

通过以上配置和对比,您可以根据项目需求选择最合适的框架。

http://www.xdnf.cn/news/109639.html

相关文章:

  • (即插即用模块-特征处理部分) 四十一、(2024) MSAA 多尺度注意力聚合模块
  • 我的独立开发技术栈
  • 未曾设想的道路1
  • Ubuntu22.04新版本谷歌无法使用搜狗输入法/中文不显示
  • 三、Python编程基础03
  • 使用Python模拟子弹与子弹的碰撞
  • 四神-华夏大地的守护神
  • 【AI】MCP,弥补Agent的缺陷
  • 一个关于相对速度的假想的故事-7
  • “该虚拟机似乎正在使用中“
  • HCIP实验二(OSPF网络配置与优化)
  • 智能指针(weak_ptr )之三
  • MySQL 8.4企业版 安装和配置审计插件
  • 大模型Agent
  • 第十七讲、Isaaclab中使用操作空间控制器
  • Vue3:watch(监听事件)
  • 【阿里云大模型高级工程师ACP习题集】2.3 优化提示词改善答疑机器人回答质量
  • 基于javaweb的SpringBoot扶农助农平台管理系统设计与实现(源码+文档+部署讲解)
  • 【OceanBase相关】01-OceanBase数据库部署实践
  • java面试题及答案2020,java最新面试题(四十四)
  • 民宿管理系统4
  • 基于ESP32 - S3的MD5校验算法的C语言例程
  • 动态显微镜数据分析及AI拓展
  • 字典树(前缀树)的实现(5)0423
  • Linux: 进程的调度
  • spring-session-data-redis使用
  • # 深度学习中的学习率调度:以 PyTorch 图像分类为例
  • 扣子空间试用:生成五一骑行规划+notion文章编写
  • 青少年编程与数学 02-018 C++数据结构与算法 06课题、树
  • 2022 年 9 月青少年软编等考 C 语言七级真题解析