SpringBoot使用swagger生成api接口文檔的方法詳解
前言
在之前的文章中,使用mybatis-plus生成了對(duì)應(yīng)的包,在此基礎(chǔ)上,我們針對(duì)項(xiàng)目的api接口,添加swagger配置和注解,生成swagger接口文檔
具體可以查看本站spring boot系列文章:
spring boot項(xiàng)目使用mybatis-plus代碼生成實(shí)例
具體例子
maven配置
在使用之前,我們需要添加swagger中maven相關(guān)依賴配置
<!--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>
項(xiàng)目application.yml配置
swagger: basePackage: com.lewyon.mybatislewyon #包名 title: 標(biāo)題 #標(biāo)題 description: lewyon #描述 version: V1.0 #版本號(hào)
以上配置包含了swagger文檔展示的包名,標(biāo)題以及描述,版本號(hào)等信息
springApplication添加swagger注解
在springApplication添加swagger注解之后,項(xiàng)目啟動(dòng)時(shí),會(huì)注入swagger相關(guān)配置和代碼,
項(xiàng)目啟動(dòng)成功之后
服務(wù)地址/swagger-ui.html就是當(dāng)前swagger文檔地址
當(dāng)前項(xiàng)目是: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 常用于描述當(dāng)前Rest的模塊信息
ApiOperation 則是當(dāng)前方法的信息
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); } }
到此這篇關(guān)于SpringBoot使用swagger生成api接口文檔的方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot swagger生成api接口文檔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot mybatisplus如何解決分頁(yè)組件IPage失效問題
這篇文章主要介紹了Springboot mybatisplus如何解決分頁(yè)組件IPage失效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08jcrop 網(wǎng)頁(yè)截圖工具(插件)開發(fā)
今天給大家介紹一下一個(gè)web 中經(jīng)常會(huì)用到的截圖(如:頭像等)工具,需要的朋友可以了解下2012-11-11Spring(二):Spring通過IOC來創(chuàng)建對(duì)象
下面小編就為大家?guī)硪黄斦凷pring對(duì)IOC的理解(推薦篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-07-07基于spring?data?jpa?@query返回map的踩坑記錄
這篇文章主要介紹了基于spring?data?jpa?@query返回map的踩坑記錄,具有很好的參考價(jià)值,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11基于SSM框架實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的示例代碼
這篇文章主要介紹了基于SSM框架實(shí)現(xiàn)簡(jiǎn)單的登錄注冊(cè)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12