前言

在之前的文章中,使用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. 以太坊 layer2: optimism 源码学习 (一)

    作者:林冠宏 / 指尖下的幽灵.转载者,请: 务必标明出处. 掘金:https://juejin.im/user/1785262612681997 博客:http://www.cnblogs.com/ ...

  2. typora的第一天

    一级标题 二级标题 三级标题 ..... 表格 java spring mybatis 代码 java代码 public void Hello(){ } 字体 hello word! hello wo ...

  3. MySQL数据库的创建和基本的查询语句

    数据库的定义 数据库是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库 分类 非结构化数据: 数据相对来说没有固定的特点 半结构化数据: 数据之间有着相同的存储结构 属性 值 每一条数 ...

  4. CSS(上)

    css sprite是什么,有什么优缺点 概念:将多个小图片拼接到⼀个图⽚中.通过 background-position 和元素尺寸调节需要显示的背景图案. 优点: 减少 HTTP 请求数,极⼤地提 ...

  5. Luogu3904 三只小猪 (组合数学,第二类斯特林数,高精)

    即使\(n<=50\),斯特林数也会爆long long. #include <iostream> #include <cstdio> #include <cstr ...

  6. 腾讯云服务器安装rabbitmq及简单测试记录

    一.安装 近期对消息队列的学习让我对消息中间件产生了兴趣,而作为消息队列的使用的rabbitmq,就是我学习中间件的第一步. rabbitmq和erlang的安装 安装之前先检查环境: [root@V ...

  7. 跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

    摘要:本篇文章结合灰度三维图像讲解图像顶帽运算和图像黑猫运算,通过Python调用OpenCV函数实现. 本文分享自华为云社区<[Python图像处理] 十三.基于灰度三维图的图像顶帽运算和黑帽 ...

  8. docker启动失败问题

    内核3.10,systemctl start docker 被阻塞,没有返回,查看状态为启动中. 某兄弟机器安装docker之后,发现systemctl start docker的时候阻塞,由于排查走 ...

  9. OpenJudge 1.5.24 正常血压

    24:正常血压 总时间限制: 1000ms 内存限制: 65536kB 描述 监护室每小时测量一次病人的血压,若收缩压在90 - 140之间并且舒张压在60 - 90之间(包含端点值)则称之为正常,现 ...

  10. Neo4j在linux上的安装与Springboot的集成

    Neo4j在linux上的安装与Springboot的集成 在linux安装: 前提:安装配置好java环境 1.下载neo4j 官方社区版下载地址:https://neo4j.com/downloa ...