前言

在之前的文章中,使用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接口文档的更多相关文章

  1. Spring Boot2配置Swagger2生成API接口文档

    一.Swagger2介绍 前后端分离开发模式中,api文档是最好的沟通方式. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 及时性 (接 ...

  2. .netcore Swagger 生成 api接口文档

    1, 引用第三方包, Swashbuckle.AspNetCore Swashbuckle.AspNetCore.Swagger Swashbuckle.AspNetCore.SwaggerUI 最简 ...

  3. .NET Core WEB API使用Swagger生成在线接口文档

    1项目引用Swashbuckle.AspNetCore程序集和Microsoft.Extensions.PlatformAbstractions程序集 右击项目打开"管理NuGet程序包.. ...

  4. SpringBoot + Swagger2 自动生成API接口文档

    spring-boot作为当前最为流行的Java web开发脚手架,相信越来越多的开发者会使用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于移动端 ...

  5. Spring MVC学习总结(9)——Spring MVC整合swagger自动生成api接口文档

    Swagger 号称:世界最流行的API框架,官网:http://swagger.io/,Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总 ...

  6. 使用Swagger生成简单接口文档

    使用swagger通过简单的配置可以生成简单的接口文档: 依赖包: // Swagger2 compile 'io.springfox:springfox-swagger2:2.8.0' compil ...

  7. SpringBoot18 Swagger、API接口文档生成、WireMock、模拟后台数据

    1 Swagger 1.1 简述 前后端分离的项目需要前后端开发人员协同工作,后台开发人员需要给到前端开发者一套API文档:利用Swagger可以简单高效的帮助后台开发者生成RestfulAPI开发文 ...

  8. .NET Core使用swagger进行API接口文档管理

    一.问题背景 随着技术的发展,现在的开发模式已经更多的转向了前后端分离的模式,在前后端开发的过程中,联系的方式也变成了API接口,但是目前项目中对于API的管理很多时候还是通过手工编写文档,每次的需求 ...

  9. swagger 生成的接口文档,隐藏接口的某个参数

    [问题描述] controller 中的处理请求的方法,有时候会添加一些额外的参数.比如下面代码中 UserVo: @PostMapping(value = "/add-office-par ...

随机推荐

  1. 如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然

    最近一键"露齿笑"席卷全网,无论是短视频用户还是社交App用户都在使用这项黑科技.当三两好友聚会拍集体照留念时,为了处理个别人的表情"瑕疵",让大家都尽量保持微 ...

  2. 2535-springsecurity系列--关于授权角色“ROLE”前缀的问题

    版本信息 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...

  3. LevelSequence源码分析

    前言 这篇文章主要讲的是Unreal LevelSequence RunTime的部分.即在游戏中运行Level Sequence的源码解析.(而且抛去Replicated 的Sequence,一般S ...

  4. Java学习 (六)基础篇 类型转换

    类型转换 由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换 字节大小(容量)-> 低--------------------------------------------- ...

  5. GreatSQL MGR FAQ

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 目录 0. GreatSQL简介 1. GreatSQL的特色有哪些 2. Gr ...

  6. 用VS Code搞Qt6:至简窗口部件——QWidget

    在正题开始之前,老周照例扯点别的.嗯,咱们扯一下在 VS 2022 下结合 CMake 开发 Qt6 时的环境变量设置问题.在VS Code 中,通够通过 CMake Tools 扩展的配置来设置环境 ...

  7. 前端必备的 HTTP 知识

    HTTP 起源 HTTP 是由蒂姆·伯纳斯-李(TimBerners-Lee)于1989年在欧洲核子研究组织(CERN)所发起 其中最著名的是 1999 年 6 月公布的 RFC 2616,定义了 H ...

  8. Linux 基于源码安装 Redis

    1.下载 Redis: 前往 Redis 官网复制 Redis 相应版本的下载链接,到终端下载 2. 进入到指定目录, 下载 redis.tar.gz 包,运行 wget + 复制的下载链接  例如: ...

  9. 【python】pandas 索引操作

    选择.修改数据(单层索引) 推荐使用.at..iat..loc..iloc 操作 句法 结果 备注 选择列 df[col] Series 基于列名(列的标签),返回Series 用标签选择行 df.l ...

  10. 算法模板:spfa

    #include<iostream> #include<algorithm> #include<cstring> #include<string> #i ...