spring boot使用swagger生成api接口文档
2022/10/16 4:24:00
本文主要是介绍spring boot使用swagger生成api接口文档,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前言
在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档
具体可以查看本站spring boot系列文章:
spring boot项目使用mybatis-plus代码生成实例
具体例子
maven配置
在使用之前,我们需要添加swagger中maven相关依赖配置
<!--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>
项目application.yml配置
swagger: basePackage: com.lewyon.mybatislewyon #包名 title: 标题 #标题 description: lewyon #描述 version: V1.0 #版本号
以上配置包含了swagger文档展示的包名,标题以及描述,版本号等信息
springApplication添加swagger注解
在springApplication添加swagger注解之后,项目启动时,会注入swagger相关配置和代码,
项目启动成功之后
服务地址/swagger-ui.html就是当前swagger文档地址
当前项目是:http://localhost:8080/swagger-ui.html
package com.lewyon.mybatislewyon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @SpringBootApplication public class MybatislewyonApplication { public static void main(String[] args) { SpringApplication.run(MybatislewyonApplication.class, args); } }
在控制层添加swagger注解
Api 常用于描述当前Rest的模块信息
ApiOperation 则是当前方法的信息
package com.lewyon.mybatislewyon.user.controller; import com.lewyon.mybatislewyon.user.entity.User; import com.lewyon.mybatislewyon.user.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * <p> * 前端控制器 * </p> * * @author lewyon * @since 2022-06-25 */ @RestController @RequestMapping("/user") @Api(value = "用户", tags = {"用户操作"}) public class UserController { @Autowired UserService userService; @GetMapping("/list") @ApiOperation("用户列表") public List<User> listUser() { return userService.list(); } @GetMapping("/getUser/{userId}") @ApiOperation("用户详情") public User getUserById(@PathVariable long userId) { return userService.getById(userId); } @GetMapping("/updateUser/{user}") @ApiOperation("更新用户") public boolean updateUserById(User user) { return userService.updateById(user); } @GetMapping("/addUser/{user}") @ApiOperation("新增用户") public boolean addUser(User user) { return userService.save(user); } @GetMapping("/deleteUser/{id}") @ApiOperation("删除用户") public boolean delUserById(String id) { return userService.removeById(id); } }
总结
以上就是spring boot集成swagger生成接口文档的例子,关于swagger更多配置,可以查阅swagger官方文档
swagger官方文档
文章个人博客地址:
spring boot使用swagger生成api接口文档
项目源码地址:
https://gitee.com/lewyon/spring-note
项目源码包含了swagger,后续更新关于spring boot集成swagger基础实例
这篇关于spring boot使用swagger生成api接口文档的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-20测试人员都是画画大神,让我看看谁还不会用代码图?
- 2024-05-20年薪百万的程序员都在用的摸鱼方式……
- 2024-05-19永别了,微服务架构!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了