当前位置: 首页 > 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

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

相关文章:

  • JavaScript 渲染内容爬取:Puppeteer 高级技巧与实践
  • 服务器-conda下载速度慢-国内源
  • Unity进阶课程【五】WebGL 打包文件本地运行报错解决 - 局域网、无限制人数、本地服务
  • 【白雪讲堂】GEO优化第6篇 内容中台的搭建:GEO优化的中控神经系统
  • 使用 Conda 创建新环境
  • MAGI-1自回归式大规模视频生成
  • Linux的进程间通信
  • Docker配置带证书的远程访问监听
  • 身份证实名认证:通往数字安全与便捷生活的钥匙
  • 璞华ChatBI闪耀2025数博会:对话式数据分析引领数智化转型新范式
  • Jmeter中同步定时器使用注意点
  • 元素滚动和内容垂直居中同时存在,完美的 html 元素垂直居中的方法flex + margin: auto
  • IP地址与子网掩码
  • IDEA add gitlab account 提示
  • Windows 同步技术-一次性初始化
  • 一文读懂https
  • 系统分析师第八、九章
  • 管理100个小程序-很难吗
  • 【源码分析】Linux内核ov13850.c
  • 异构迁移学习(无创脑机接口中的跨脑电帽迁移学习)
  • 俄总理:2024年俄罗斯GDP增长4.3%
  • 马上评丨电子屏不如黑板?解决问题不能靠怀旧
  • 巴勒斯坦民族权力机构主席:哈马斯必须移交武器
  • 吉祥航空去年净利增超17%,海航实控人方威退出前十大股东
  • 2025年度沪惠保参保今开启:保费不变,国内特药种类扩增
  • 一季度全国纪检监察机关共处分18.5万人,其中省部级干部14人