SpringBoot-7. 使用Swagger构建文档
swagger可以降低我们前后端开发文档同步问题,其能够根据我们的代码注释自动生成API文档,而我们只需简单的运用其注释即可,可视化的呈现程序所有的接口列表,非常方便我们前后端进行接口调试。
The Best APIs are Built with Swagger Tools
Swagger工具可以帮助您完成生成和维护API文档的工作,确保您的文档在API发展过程中保持最新状态。
1. 添加Swagger依赖
<!-- swagger --> <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>
2. 新建SwaggerConfig类
3. 添加文档内容
用于Controller(控制类)
用于model(实体类)
-
官方注解文档:
4. 访问验证
http://localhost:8080/swagger-ui.html#/
效果如下: