当前位置: 首页 > news >正文

swagger 3.0 学习笔记

引入pom

        <dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency>

配置

import io.swagger.models.auth.In;
import io.swagger.v3.oas.annotations.Operation;
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.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.List;@Configuration
public class SwaggerConfig
{/*** 安全模式,这里指定token通过Authorization头请求头传递*/private List<SecurityScheme> securitySchemes(){List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue()));apiKeyList.add(new ApiKey("Head", "Head", In.HEADER.toValue()));apiKeyList.add(new ApiKey("Query", "Query", In.QUERY.toValue()));return apiKeyList;}/*** 安全上下文*/private List<SecurityContext> securityContexts(){List<SecurityContext> securityContexts = new ArrayList<>();securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).operationSelector(o -> o.requestMappingPattern().matches("/.*")).build());return securityContexts;}/*** 默认的安全上引用*/private List<SecurityReference> defaultAuth(){AuthorizationScope[] authorizationScopes ={new AuthorizationScope("global", "accessEverything")} ;List<SecurityReference> securityReferences = new ArrayList<>();securityReferences.add(new SecurityReference("Authorization", authorizationScopes));securityReferences.add(new SecurityReference("Head", authorizationScopes));securityReferences.add(new SecurityReference("Query", authorizationScopes));return securityReferences;}@Beanpublic Docket swagger3() {return new Docket(DocumentationType.OAS_30).select().apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class)).paths(PathSelectors.regex("/test.*")).build().groupName("swagger3.0").securitySchemes(securitySchemes()).securityContexts(securityContexts()).apiInfo(apiInfo());}/*** 添加摘要信息*/private ApiInfo apiInfo(){// 用ApiInfoBuilder进行定制return new ApiInfoBuilder()// 设置标题.title("标题:XXX系统_接口文档")// 描述.description("描述:xxxx.")// 作者信息.contact(new Contact("作者信息", null, null))// 版本.version("版本号: >...>").build();}
}

可以加上全局请求参数
在这里插入图片描述

对象

ReqSwagger3VO

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;import java.io.Serializable;
import java.util.List;@Data
@Schema(name="ReqSwagger3VO",description ="swagger3对象" )
public class ReqSwagger3VO implements Serializable {private static final long serialVersionUID = 646541L;@Schema(description = "字符串",example = "aaaa")private String dataStr;@Schema(description = "数字",example = "1111")private Integer dataInt;@Schema(description = "字符数组" )private List<String> listStr;@Schema(description = "数字数组")private List<Integer> listInt;@Schema(description = "用户")private User user;@Schema(description = "用户数组")private  List<User> userList;@Schema(hidden = true)private String hiddenStr;private String hiddenNotStr;
}

User

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
//https://www.cnblogs.com/antLaddie/p/17418078.html
@Schema(description ="用户信息" )
@Data
@AllArgsConstructor
public class User {@Schema(description = "姓名",example = "张三")private String name;@Schema(description = "年龄",example = "18", format = "int32")private int age;@Schema( description = "学生分数属性", format = "double", example = "55.50")private Double fraction;
}

控制层

example 赋值问题还没解决


import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;@RestController
@RequestMapping("/test/swagger")
@Tag(name = "接口类描述name",description = "接口类描述desc")
public class SwaggerController {@Operation(summary = "get请求")@GetMapping("{pathParam}/get")public Object get(@Parameter(description="reqParam参数描述",  schema = @Schema(allowableValues = {"aaa", "22222"}))@RequestParam String reqParam,@Parameter(description="pathParam参数描述",example = "111")@PathVariable Integer pathParam,@Parameter(description="headParam参数描述", schema = @Schema(allowableValues = {"888888"}))@RequestHeader Long headParam){return null;}@Operation(summary = "get2请求,example赋值不生效",parameters={@Parameter(ref="reqParam",name = "reqParam",description="reqParam参数描述",in= ParameterIn.QUERY,  schema = @Schema(allowableValues = {"aaa", "22222"})),@Parameter(ref="pathParam", name = "pathParam",description="pathPatam参数描述",in= ParameterIn.PATH,schema = @Schema(allowableValues = {"11111", "333"})),@Parameter(ref="headParam", name = "headParam",description="headParam参数描述",in= ParameterIn.PATH,schema = @Schema(allowableValues = {"11111", "333"}))})@GetMapping("/2/{pathParam}/get")public Object get2(@RequestParam String reqParam,@PathVariable Integer pathParam ,@RequestHeader Long headParam){return null;}@Operation(summary = "post请求")@PostMapping("/post")public Object post(@RequestBody ReqSwagger3VO reqSwaggerVO){return null;}@Hidden@PostMapping("/hidden")public Object hidden(@RequestBody ReqSwaggerVO reqSwaggerVO){return null;}@Operation(summary = "文件上传")@PostMapping(value = "/upload")public Object uploadFile(@RequestParam(value = "file") @RequestPart MultipartFile file){return null;}@Operation(summary = "zip下载")@PostMapping(value = "/zip/download")public void downloadZip(HttpServletResponse response)throws Exception {String fileName ="file.zip";ByteArrayOutputStream outputStream = new ByteArrayOutputStream();ZipOutputStream zip = new ZipOutputStream(outputStream);for (int i = 0; i < 2; i++) {StringWriter sw = new StringWriter();sw.append("11111a"+i);sw.append("2222c"+i);// 添加到zipzip.putNextEntry(new ZipEntry(i+"name.txt"));IOUtils.write(sw.toString(), zip, "UTF-8");IOUtils.close(sw);zip.flush();zip.closeEntry();}IOUtils.close(zip);byte[] data = outputStream.toByteArray();response.reset();response.addHeader("Access-Control-Allow-Origin", "*");response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");response.setHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");response.addHeader("Content-Length", "" + data.length);response.setContentType("application/octet-stream; charset=UTF-8");IOUtils.write(data, response.getOutputStream());}}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

相关文章:

swagger 3.0 学习笔记

引入pom <dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency>配置 import io.swagger.models.auth.In; import io.swagger.v3.oas.annotati…...

07 |「异步任务」

前言 实践是最好的学习方式&#xff0c;技术也如此。 文章目录 前言一、进程与线程1、进程2、线程 二、实现 一、进程与线程 1、进程 进程(Process)是操作系统分配资源的基本单位,它是一个执行中的程序实例&#xff1b;每个进程都有自己独立的内存空间,不同进程的内存是相互独…...

LoRaWan网关设计之入门指南

快速开始 以下是在目标平台本身上构建和运行 LoRaWan网关 的三步快速入门指南。 第 1 步:克隆 网关源码库 git clone https://github.com/lorabasics/basicstation.git...

互联网电影购票选座后台管理系统源码开发

搭建一个互联网电影购票选座后台管理系统需要进行以下步骤&#xff1a; 1. 需求分析&#xff1a;首先要明确系统的功能和需求&#xff0c;包括电影列表管理、场次管理、座位管理、订单管理等。 2. 技术选型&#xff1a;选择适合的技术栈进行开发&#xff0c;包括后端开发语言…...

[ K8S ] yaml文件讲解

目录 查看 api 资源版本标签写一个yaml文件demo创建资源对象查看创建的pod资源创建service服务对外提供访问并测试//创建资源对象查看创建的service写yaml太累怎么办&#xff1f; Kubernetes 支持 YAML 和 JSON 格式管理资源对象 JSON 格式&#xff1a;主要用于 api 接口之间消…...

【《深入浅出计算机网络》学习笔记】第1章 概述

内容来自b站湖科大教书匠《深入浅出计算机网络》视频和《深入浅出计算机网络》书籍 目录 1.1 信息时代的计算机网络 1.1.1 计算机网络的各类应用 1.1.2 计算机网络带来的负面问题 1.2 因特网概述 1.2.1 网络、互联网与因特网的区别与关系 1.2.1.1 网络 1.2.1.2 互联网 …...

二、Linux中权限、shell命令及运行原理

shell命令及运行原理 我们使用Linux时&#xff0c;并不是直接访问操作系统&#xff0c;为什么不是直接访问操作系统呢&#xff1f; 如果用户直接访问操作系统&#xff0c;不仅使用难度大&#xff0c;而且不安全&#xff0c;容易把系统文件损坏。 那么我们通常是如何访问操作系统…...

【RabbitMQ上手——单实例安装5种简单模式实现通讯过程】

【RabbitMQ入门-单实例安装&5种简单模式实现通讯过程】 一、环境说明二、安装RabbitMQ三、用户权限及Virtual Host设置四、5种简单模式实现通讯过程的实现五、小结 一、环境说明 安装环境&#xff1a;虚拟机VMWare Centos7.6 Maven3.6.3 JDK1.8RabbitMQ版本&#xff1a;…...

python+pytest接口自动化之HTTP协议基础

HTTP协议简介 HTTP 即 HyperText Transfer Protocol&#xff08;超文本传输协议&#xff09;&#xff0c;是互联网上应用最为广泛的一种网络协议。所有的 WWW 文件都必须遵守这个标准。 设计 HTTP 最初的目的是为了提供一种发布和接收 HTML 页面的方法。HTTP 协议在 OSI 模型…...

【技巧】如何保护PowerPoint不被改动?

PPT&#xff0c;也就是PowerPoint&#xff0c;是很多小伙伴在工作生活中经常用到的图形演示文稿软件。 做好PPT后&#xff0c;担心自己不小心改动了或者不想他人随意更改&#xff0c;我们可以如何保护PPT呢&#xff1f;下面小编就来分享两个常用的方法&#xff1a; 1. 将PPT改…...

【APITable】教程:创建并运行一个自建小程序

1.进入APITable&#xff0c;在想要创建小程序的看板页面点击右上角的【小程序】&#xff0c;进入小程序编辑页面。 2.创建一个新的小程序区。 点击【 添加小程序】 点击创建小程序&#xff0c;选择模板&#xff0c;输入名字。 3.确定后进入小程序部署引导页面。 4.打开Xshell 7…...

使用MyBatis操作数据库

hi,大家好,今天为大家带来MyBatis操作数据库的知识 文章目录 &#x1f437;1.根据MyBatis操作数据库&#x1f9ca;1.1查询操作&#x1f347;1.1.1无参查询&#x1f347;1.1.2有参查询 &#x1f9ca;1.2删除操作&#x1f9ca;1.3修改操作&#x1f9ca;1.4增加操作&#x1f9ca;…...

SSM(Vue3+ElementPlus+Axios+SSM前后端分离)--功能实现[五]

文章目录 SSM--功能实现实现功能09-带条件查询分页显示列表需求分析/图解思路分析代码实现测试分页条件查询带条件分页查询显示效果 实现功能10-添加家居表单前端校验需求分析/图解思路分析代码实现完成测试测试页面效果 实现功能11-添加家居表单后端校验需求分析/图解思路分析…...

Qt应用程序窗体最大化失效问题的解决方法

记录一个在Qt开发过程中遇到的问题&#xff1a; 【问题描述】在showEvent中调用showMaximized()&#xff0c;应用程序窗体仍然无法最大化。 【定位分析】在Qt应用程序中&#xff0c;如果窗体最大化失效&#xff0c;可能是因为在窗体的showEvent事件中使用了showMaximized()方…...

python怎么判断变量的数据类型

在编程的世界里&#xff0c;了解各种数据类型是非常重要的。在Python中&#xff0c;有着丰富的数据类型用于存储和处理不同类型的数据。掌握这些数据类型的定义和作用&#xff0c;我们能够更好地在程序中管理和操作数据&#xff0c;提高代码的效率和可读性。 Python中常见的数据…...

FastAPI 构建 API 高性能的 web 框架(二)

上一篇 FastAPI 构建 API 高性能的 web 框架&#xff08;一&#xff09;是把LLM模型使用Fastapi的一些例子&#xff0c;本篇简单来看一下FastAPI的一些细节。 有中文官方文档&#xff1a;fastapi中文文档 假如你想将应用程序部署到生产环境&#xff0c;你可能要执行以下操作&a…...

如何实现 Java SpringBoot 自动验证入参数据的有效性

Java SpringBoot 通过javax.validation.constraints下的注解&#xff0c;实现入参数据自动验证 如果碰到 NotEmpty 否则不生效&#xff0c;注意看下 RequestBody 前面是否加上了Valid Validation常用注解汇总 Constraint详细信息Null被注释的元素必须为 nullNotNull被注释的元…...

golang学习随记

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 go学习快捷键及快速生成代码片段go基础循环流程控制关键字切片&#xff0c;拷贝函数闭包 defer语句格式化输出go语言随机数rand.seed() 包管理并发编程goroutinecha…...

【PCL-6】PCL基于凹凸型的分割算法(LCCP)

凹凸型分割算法适用于颜色类似、棱角分明的物体场景分割。LCCP方法不依赖点云颜色&#xff0c;只使用空间信息和法线信息。 算法流程&#xff1a; 1、基于超体聚类的过分割&#xff1b; 2、在超体聚类的基础上再聚类。 算法思路&#xff1a; 1、基于CC和SC判断凹凸性&…...

多进程并发服务器

文章目录 思路问题多进程并发回环服务器代码客户端代码 思路 每当一个客户端连接服务器后&#xff0c;创建一个子进程负责与该客户端通信&#xff0c;客户端断开连接之后&#xff0c;服务器回收子进程资源。 问题 问题1&#xff1a;父进程阻塞在等待连接(accept())处&#xf…...

VB.net复制Ntag213卡写入UID

本示例使用的发卡器&#xff1a;https://item.taobao.com/item.htm?ftt&id615391857885 一、读取旧Ntag卡的UID和数据 Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click轻松读卡技术支持:网站:Dim i, j As IntegerDim cardidhex, …...

解锁数据库简洁之道:FastAPI与SQLModel实战指南

在构建现代Web应用程序时&#xff0c;与数据库的交互无疑是核心环节。虽然传统的数据库操作方式&#xff08;如直接编写SQL语句与psycopg2交互&#xff09;赋予了我们精细的控制权&#xff0c;但在面对日益复杂的业务逻辑和快速迭代的需求时&#xff0c;这种方式的开发效率和可…...

高频面试之3Zookeeper

高频面试之3Zookeeper 文章目录 高频面试之3Zookeeper3.1 常用命令3.2 选举机制3.3 Zookeeper符合法则中哪两个&#xff1f;3.4 Zookeeper脑裂3.5 Zookeeper用来干嘛了 3.1 常用命令 ls、get、create、delete、deleteall3.2 选举机制 半数机制&#xff08;过半机制&#xff0…...

#Uniapp篇:chrome调试unapp适配

chrome调试设备----使用Android模拟机开发调试移动端页面 Chrome://inspect/#devices MuMu模拟器Edge浏览器&#xff1a;Android原生APP嵌入的H5页面元素定位 chrome://inspect/#devices uniapp单位适配 根路径下 postcss.config.js 需要装这些插件 “postcss”: “^8.5.…...

【MATLAB代码】基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),附源代码|订阅专栏后可直接查看

文章所述的代码实现了基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),针对传感器观测数据中存在的脉冲型异常噪声问题,通过非线性加权机制提升滤波器的抗干扰能力。代码通过对比传统KF与MCC-KF在含异常值场景下的表现,验证了后者在状态估计鲁棒性方面的显著优…...

Caliper 配置文件解析:fisco-bcos.json

config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...

android RelativeLayout布局

<?xml version"1.0" encoding"utf-8"?> <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"match_parent"android:gravity&…...

Spring Security 认证流程——补充

一、认证流程概述 Spring Security 的认证流程基于 过滤器链&#xff08;Filter Chain&#xff09;&#xff0c;核心组件包括 UsernamePasswordAuthenticationFilter、AuthenticationManager、UserDetailsService 等。整个流程可分为以下步骤&#xff1a; 用户提交登录请求拦…...

【深度学习新浪潮】什么是credit assignment problem?

Credit Assignment Problem(信用分配问题) 是机器学习,尤其是强化学习(RL)中的核心挑战之一,指的是如何将最终的奖励或惩罚准确地分配给导致该结果的各个中间动作或决策。在序列决策任务中,智能体执行一系列动作后获得一个最终奖励,但每个动作对最终结果的贡献程度往往…...

智警杯备赛--excel模块

数据透视与图表制作 创建步骤 创建 1.在Excel的插入或者数据标签页下找到数据透视表的按钮 2.将数据放进“请选择单元格区域“中&#xff0c;点击确定 这是最终结果&#xff0c;但是由于环境启不了&#xff0c;这里用的是自己的excel&#xff0c;真实的环境中的excel根据实训…...