Swagger API文档:Java RESTful服务的自动生成

Swagger 简介

Swagger 是一套用于设计、构建、文档化和消费 RESTful API 的开源工具集。通过 Swagger,可以自动生成 API 文档,并提供交互式测试界面。

集成 Swagger 到 Java RESTful 服务

添加 Maven 依赖

pom.xml 文件中添加以下依赖:

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>
配置 Swagger

创建 Swagger 配置类:

java 复制代码
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("RESTful API 文档")
                .description("Java RESTful 服务接口")
                .version("1.0")
                .build();
    }
}
启用 Swagger UI

启动应用后,访问 http://localhost:8080/swagger-ui.html 即可查看自动生成的 API 文档并进行测试。

常用 Swagger 注解

接口描述
  • @Api:标注控制器类,提供接口分组信息。
  • @ApiOperation:标注方法,描述接口功能。
  • @ApiParam:标注方法参数,说明参数含义。
数据模型
  • @ApiModel:标注实体类,说明模型用途。
  • @ApiModelProperty:标注字段,描述属性信息。

示例代码

控制器示例
java 复制代码
@RestController
@RequestMapping("/api/users")
@Api(tags = "用户管理")
public class UserController {
    @GetMapping("/{id}")
    @ApiOperation("根据 ID 查询用户")
    public User getUser(@PathVariable @ApiParam("用户 ID") Long id) {
        return userService.getUserById(id);
    }
}
实体类示例
java 复制代码
@ApiModel("用户信息")
public class User {
    @ApiModelProperty("用户 ID")
    private Long id;

    @ApiModelProperty("用户名")
    private String name;
}

升级到 OpenAPI 3.0

如需使用 Swagger 3.0(OpenAPI 3.0),可替换依赖为:

XML 复制代码
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.12</version>
</dependency>

访问地址变更为 modelscope.cn/learn/71516
modelscope.cn/learn/71514
modelscope.cn/learn/71512
modelscope.cn/learn/71510
modelscope.cn/learn/71508
modelscope.cn/learn/71506
modelscope.cn/learn/71504
modelscope.cn/learn/71502
modelscope.cn/learn/71500
modelscope.cn/learn/71498
modelscope.cn/learn/71496
modelscope.cn/learn/71493
modelscope.cn/learn/71492
modelscope.cn/learn/71489
modelscope.cn/learn/71488
modelscope.cn/learn/71485
modelscope.cn/learn/71484
modelscope.cn/learn/71482
modelscope.cn/learn/71480
modelscope.cn/learn/71478
modelscope.cn/learn/71476
modelscope.cn/learn/71474
modelscope.cn/learn/71472
modelscope.cn/learn/71470
modelscope.cn/learn/71469
modelscope.cn/learn/71466
modelscope.cn/learn/71465
modelscope.cn/learn/71462
modelscope.cn/learn/71460
modelscope.cn/learn/71458
modelscope.cn/learn/71456
modelscope.cn/learn/71454
modelscope.cn/learn/71452
modelscope.cn/learn/71450
modelscope.cn/learn/71448
modelscope.cn/learn/71445
modelscope.cn/learn/71444
modelscope.cn/learn/71441
modelscope.cn/learn/71440
modelscope.cn/learn/71438
modelscope.cn/learn/71436
modelscope.cn/learn/71434
modelscope.cn/learn/71432
modelscope.cn/learn/71430
modelscope.cn/learn/71427
modelscope.cn/learn/71426
modelscope.cn/learn/71423
modelscope.cn/learn/71422
modelscope.cn/learn/71419
modelscope.cn/learn/71418

总结

Swagger 能够大幅提升 API 开发效率,自动生成文档并支持在线测试。通过合理使用注解,可以增强文档的可读性和实用性。

相关推荐
G.晴天1 小时前
Linux常用命令练习流程
java·linux·运维·服务器·tomcat
身如柳絮随风扬2 小时前
Java对象在计算机中的执行原理:从JVM内存模型到对象创建全过程
java·开发语言·jvm
夕除2 小时前
spring boot
java·spring boot·后端
想唱rap2 小时前
传输层协议之UDP
java·linux·网络·c++·网络协议·mysql·udp
河西石头2 小时前
听AI的血的教训!PPOCRLabel部署与PyQt5的安装避坑-百分百成功!
开发语言·人工智能·python·pyqt5安装·ppocrlabel的部署
野生技术架构师2 小时前
我总结了这份2026最新版Java面试题库(背完这一套就够了)
java·开发语言·面试
AIGC设计所2 小时前
网络安全8大就业领域和待遇对比!
运维·开发语言·网络·安全·web安全·php
xxjj998a2 小时前
PHP与汇编:从Web到硬件的编程差异
开发语言·汇编·php
可爱の小公举2 小时前
Java 后端程序员转 AI Agent 工程师:一条可执行学习路线
java·人工智能·学习