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)依賴(lài)配置
<!--swagger 接口說(shuō)明文檔框架--> <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 = "用戶(hù)", tags = {"用戶(hù)操作"}) public class UserController { @Autowired UserService userService; @GetMapping("/list") @ApiOperation("用戶(hù)列表") public List<User> listUser() { return userService.list(); } @GetMapping("/getUser/{userId}") @ApiOperation("用戶(hù)詳情") public User getUserById(@PathVariable long userId) { return userService.getById(userId); } @GetMapping("/updateUser/{user}") @ApiOperation("更新用戶(hù)") public boolean updateUserById(User user) { return userService.updateById(user); } @GetMapping("/addUser/{user}") @ApiOperation("新增用戶(hù)") public boolean addUser(User user) { return userService.save(user); } @GetMapping("/deleteUser/{id}") @ApiOperation("刪除用戶(hù)") 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)文章
Java Spring中Quartz調(diào)度器詳解及實(shí)例
這篇文章主要介紹了Java Spring中Quartz調(diào)度器詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02SpringBoot實(shí)現(xiàn)日志鏈路追蹤的項(xiàng)目實(shí)踐
在分布式系統(tǒng)中,由于請(qǐng)求的處理過(guò)程可能會(huì)跨越多個(gè)服務(wù),因此,對(duì)請(qǐng)求的追蹤變得尤為重要,本文主要介紹了SpringBoot實(shí)現(xiàn)日志鏈路追蹤的項(xiàng)目實(shí)踐,感興趣的可以了解一下2024-03-03詳解Spring中使用xml配置bean的細(xì)節(jié)
本篇文章主要介紹了Spring中使用xml配置bean的細(xì)節(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06JAVA用遞歸實(shí)現(xiàn)全排列算法的示例代碼
這篇文章主要介紹了JAVA用遞歸實(shí)現(xiàn)全排列算法的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07Java中短路運(yùn)算符與邏輯運(yùn)算符示例詳解
這篇文章主要給大家介紹了關(guān)于Java中短路運(yùn)算符與邏輯運(yùn)算符的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01用Java將字符串的首字母轉(zhuǎn)換大小寫(xiě)
在項(xiàng)目開(kāi)發(fā)的時(shí)候會(huì)需要統(tǒng)一字符串的格式,比如首字母要求統(tǒng)一大寫(xiě)或小寫(xiě),那用Java如何實(shí)現(xiàn)這一功能?下面一起來(lái)學(xué)習(xí)學(xué)習(xí)。2016-08-08