Spring Boot中如何使用Swagger详解
作者:团子大圆帅 发布时间:2023-09-27 18:13:58
目录
Swagger 简介
配置 Swagger
添加依赖
为项目开启 Swagger
创建 SwaggerConfig 配置类
访问 Swagger 前端页面
控制器相关注解
实体相关注解
总结
Swagger 简介
Swagger 是一个方便 API 开发的框架,它有以下优点:
自动生成在线文档,后端开发人员的改动可以立即反映到在线文档,并且不用单独编写接口文档,减轻了开发负担
支持通过 Swagger 页面直接调试接口,方便前端开发人员进行测试
配置 Swagger
Swagger 目前有 2.x 和 3.x 两个主流版本,配置略有不同。
添加依赖
首先去 Maven 仓库中搜索 springfox 查找依赖的坐标,Swagger 是遵循 OpenAPI 规范的技术,而 springfox 是该技术的一种实现,所以这里要搜 springfox 而不是 swagger。
对于 Swagger 2.x,需要在 pom.xml 中添加两项配置:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
对于 Swagger 3.x,简化了配置项,只需要在 pom.xml 中添加一项配置:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
为项目开启 Swagger
对于 Swagger 2.x,使用 @EnableSwagger2 注解开启 Swagger 功能。
@EnableSwagger2
@SpringBootApplication
public class SwaggerApplication {
...
}
对于 Swagger 3.x,使用 @EnableOpenApi 注解开启 Swagger 功能。
@EnableOpenApi
@SpringBootApplication
public class SwaggerApplication {
...
}
创建 SwaggerConfig 配置类
对于 Swagger 2.x,实例化 Docket 的时候,需要传入 DocumentationType.SWAGGER_2。
对于 Swagger 3.x,实例化 Docket 的时候,需要传入 DocumentationType.OAS_30。
下面是一份配置模板:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Value("${spring.profiles.active:NA}")
private String active;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // OAS_30
.enable("dev".equals(active)) // 仅在开发环境开启Swagger
.apiInfo(apiInfo())
.host("http://www.example.com") // Base URL
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API文档")
.description("这是描述信息")
.contact(new Contact("张三", null, null))
.version("1.0")
.build();
}
}
访问 Swagger 前端页面
对于 Swagger 2.x,访问 http://localhost:8080/swagger-ui.html
对于 Swagger 3.x,访问 http://localhost:8080/swagger-ui/
控制器相关注解
@Api:将一个类标记为 Swagger 资源,一般放在 Controller 类上面,通过 tags 指定描述信息,比如 @Api(tags="用户管理")。
@ApiOperation:本注解放在 Controller 方法上面,描述该方法的作用。
@ApiParam:本注解放在 Controller 方法的形参前面,可以描述参数的作用,比如 @ApiParam("用户名") String username。可以使用 value 指定描述信息,通过 required = true 指定必需传递该参数。
package com.example.swagger.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "Hello控制器")
@RestController
public class HelloController {
@ApiOperation("展示欢迎信息")
@GetMapping("/hello")
public String hello(@ApiParam("名字") String name) {
return "hello, " + name;
}
}
实体相关注解
@ApiModel:一般放在实体类上面。可以通过 value 指定别名,不指定时默认为类名。还可以通过 description 指定详细的描述信息。比如 @ApiModel("用户") 就将显示 用户 而不是 User。

如果仅仅想指定描述,而不改变原始类名显示,可以写成 @ApiModel(description = "用户")。
@ApiModelProperty:一般放在实体类的成员变量上面,通过 value 指定描述信息,example 指定示例数据,required = true 指定该参数是必需的,hidden = true 用于隐藏该字段,不会在 API 文档中显示。
package com.example.swagger.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(description = "用户")
public class User {
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private String password;
@ApiModelProperty(value = "年龄", example = "18", required = true)
private int age;
@ApiModelProperty(hidden = true)
private double money;
}
来源:https://juejin.cn/post/6992515756504121380


猜你喜欢
- 题目要求阅读理解读完题的我be like:去看了遍英文版就懂了,题目中的种类【type】不是种类数…&hell
- 本文实例为大家分享了UGUI实现卡片椭圆方向滚动的具体代码,供大家参考,具体内容如下搭建简单的场景运行效果卡片移动动画通过插件DoTween
- 一.WebSocket简单介绍WebSocket protocol 是HTML5一种新的协议。它实现了浏览器与服务器全双工通信(full-d
- 在用C#开发windows端程序并连接SQL Server时有可能会遇到数据库登录失败的问题,报错现象如下图所示:报错信息如下:System
- GridView设置如下:<asp:GridView ID="GridViewlb" runat="se
- 本文介绍了最好的Java5种遍历HashMap数据的写法,分享给大家,也给自己留一个笔记,具体如下:通过EntrySet的迭代器遍历Iter
- 本文实例讲述了Java Swing实现JTable检测单元格数据变更事件的方法。分享给大家供大家参考,具体如下:在JTable的初级教程中往
- 背景:最近项目中涉及到自定义线程池中子线程获取父线程的traceId,这个数据的传递过程可以用lamdba表达式进行封装实现的。这让我想到s
- 本文为大家整理了C#图片切割、图片压缩、缩略图生成的实现代码,大家可以收藏,方便以后使用,具体内容如下/// 图片切割函数 /// <
- 什么是适配器模式? 适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不
- 本文实例为大家分享了Java实现医院管理系统的具体代码,供大家参考,具体内容如下1.开发工具NetBeans8.2Mysql5.7mysql
- 本文实例为大家分享了Android快速实现断点续传的具体代码,供大家参考,具体内容如下1.导入依赖compile 'com.loop
- 1.首先在 build.gradle 里导入包implementation 'com.github.PhilJay:MPAndroi
- 推荐教程IntelliJ IDEA 2020最新激活码(亲测有效,可激活至 2089 年)最新idea2021注册码永久激活(激活到2100
- 对于从事Android开发的人来说,遇到ANR(Application Not Responding)是比较常见的问题。一般情况下,如果有A
- 本文实例为大家分享了C语言实现代码雨效果的具体代码,供大家参考,具体内容如下一、项目描述和最终的效果展示项目: 让字符从上到下
- 各个方法1. 得到class的成员变量首先得到object的class对象然后在class对象中用getDeclaredFields()方法
- 对流进行操作时要引用 using System.IO; 命名空间 FileStream常用的属性和方法:属性:CanRead 判断当前流是否
- 本文实例为大家分享了SpringMVC实现上传下载文件的具体代码,供大家参考,具体内容如下一、SpringMVC专门提供了CommonsMu
- TabHost控件默认使用LinearLayout包裹TabWidget和FrameLayout,布局文件如下:<TabHost xm