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());}}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/33545.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux计算机名自动变为bogon,修改计算机名

Linux计算机名自动变为bogon&#xff0c;修改计算机名 问题&#xff1a;这次机房停电&#xff0c;部分VM计算机名自动变为bogon&#xff0c;判断故障&#xff1a;因开启VM的时候&#xff0c;网卡需要获取DNS&#xff0c;但是DNS服务器还没有起来&#xff0c;故自动在resolv.con…

innodb buffer pool

buffer pool是主存中的一个区域&#xff0c;InnoDB 在访问时缓存表和索引数据。缓冲池允许直接从内存访问频繁使用的数据&#xff0c;这加快了处理速度。在专用服务器上&#xff0c;高达80% 的物理内存通常分配给缓冲池。为了提高大容量读取操作的效率&#xff0c;将缓冲池划分…

npm ERR! code ERESOLVEnpm ERR! ERESOLVE unable to resolve dependency tree

拉取项目到本地 执行 npm install 报错 遇到这个问题首先确认的就是版本是不是太高了&#xff0c;降一下版本。或者通过yarn命令替代npm install命令安装&#xff0c;同理&#xff0c;启动也可以采用yarn dev 启动代替npm run dev 下面教大家用一个NVM工具&#xff0c;这个工…

IntellIJ Idea 连接数据库-MySql

前言&#xff1a;可以用mariaDB工具&#xff0c;在本地创建服务器主机和数据库&#xff0c;而后用intellIJ Idea尝试连接 MariaDB创建数据库练习 1.IntellIJ Idea打开界面右侧Database工具&#xff0c;选择MySQL数据库。 2.填写数据库账号密码&#xff0c;地址端口号&#xff…

Kafka:springboot集成kafka收发消息

kafka环境搭建参考Kafka&#xff1a;安装和配置_moreCalm的博客-CSDN博客 1、springboot中引入kafka依赖 <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><…

无涯教程-Perl - print函数

描述 此函数将LIST中的表达式的值打印到当前的默认输出文件句柄或FILEHANDLE指定的句柄中。 如果设置,则$\变量将添加到LIST的末尾。 如果LIST为空,则打印$_中的值。 print接受一个值列表,列表中的每个元素都将被解释为一个表达式。 语法 以下是此函数的简单语法- print…

idea+gradle阅读spring5.2.9源码之源码构建报错解决方案

注意 1、先确保gradle版本和spring、jdk版本对应 本文:gradle:5.6.4/spring 5.2.9/jdk1.8&#xff08;gradle和jdk都要先安装好&#xff0c;gradle还要配置好本地资源文件路径&#xff09; 2、原来项目乱了的话&#xff0c;先重新导入下载的源码项目 3、进入源码所在根目录&…

AST入门与实战(三):if节点转switch节点(瑞数5)

原文地址:https://zhuoyue360.com/jsnx/110.html 1. 期望 这是一个瑞数5代解混淆的案例&#xff0c;我们本章节需要做的是把if节点的内容转换成switch-case内容.以此来熟悉AST对JS混淆的对抗. 原始代码: function whileState() {while (1) {aV cA[wU];if (aV < 4) {if (…

Xcode 基座打包

Xcode基座打包-APP更新版本内容无效 问题&#xff1a;解决&#xff1a; 问题&#xff1a; 使用xcode基座打包之后&#xff0c;上传到appstore进行提审发布。 用户在appstore商城进行更新下载&#xff0c;打开更新后的APP发现版本号是最新的&#xff0c;APP里面的其他内容还是上…

Node.js |(二)Node.js API:fs模块 | 尚硅谷2023版Node.js零基础视频教程

学习视频&#xff1a;尚硅谷2023版Node.js零基础视频教程&#xff0c;nodejs新手到高手 文章目录 &#x1f4da;文件写入&#x1f407;writeFile 异步写入&#x1f407;writeFileSync 同步写入&#x1f407;appendFile / appendFileSync 追加写入&#x1f407;createWriteStrea…

2. 获取自己CSDN文章列表并按质量分由小到大排序(文章质量分、博客质量分、博文质量分)(阿里云API认证)

文章目录 写在前面步骤打开CSDN质量分页面粘贴查询文章url按F12打开调试工具&#xff0c;点击Network&#xff0c;点击清空按钮点击查询是调了这个接口https://bizapi.csdn.net/trends/api/v1/get-article-score用postman测试调用这个接口&#xff08;不行&#xff0c;认证不通…

elementui实现当前页全选+所有全选+翻页保持选中状

原文来自&#xff1a;https://blog.csdn.net/sumimg/article/details/121693305?spm1001.2101.3001.6650.1&utm_mediumdistribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-121693305-blog-127570059.235%5Ev38%5Epc_relevant_anti_t3&depth_1-utm…

Linux —— 文件系统

目录 一&#xff0c;背景 二&#xff0c;文件系统 一&#xff0c;磁盘简介 磁盘分为SSD、机械磁盘&#xff1b;机械磁盘&#xff0c;即磁盘高速转动&#xff0c;磁头移动到读写扇区所在磁道&#xff0c;让磁头在目标扇区上划过&#xff0c;即可完成对扇区的读写操作&#xff…

nacos升级开启鉴权后,微服务无法连接的解决方案

版本&#xff1a; 软件版本号备注spring boot2.2.5.RELEASEspring-cloudHoxton.SR3spring-cloud-alibaba2.2.1.RELEASEnacos2.0.1从1.4.2版本进行升级。同时作为注册中心和配置中心 一、升级nacos版本&#xff0c;开启鉴权 1.在application.properties配置文件开启鉴权&…

thinkphp:分组查询(多条相同列的数据只展示一条)

例子&#xff1a;数据库中有trans_num、subinventory_from、transaction_type、creation_date有相同值&#xff0c;在查询该数据库使&#xff0c;只展示这几个值相同的一条 效果&#xff1a; 限制之前 限制之后 代码 限制前&#xff0c;后端代码 public function select_i…

Java之继承

继承 继承为什么使用继承继承是什么继承的语法访问父类成员访问父类成员变量访问父类成员方法 super关键字子类构造方法super和this异同分别的使用方法 继承的方式final关键字 作者简介&#xff1a; zoro-1&#xff0c;目前大一&#xff0c;正在学习Java&#xff0c;数据结构等…

微服务监控技术skywalking的部署与使用(亲测无坑)

微服务监控技术skywalking的部署与使用 1. 前期准备2. skywalking安装部署2.1 Java Agent2.2 apache/skywalking-oap-server2.3 apache/skywalking-ui 3. 项目启动4.效果展示 1. 前期准备 注&#xff1a;本篇文章采用docker部署&#xff0c;采用8.2.0版本&#xff0c;版本一定…

机器学习、深度学习项目开发业务数据场景梳理汇总记录三

本文的主要作用是对历史项目开发过程中接触到的业务数据进行整体的汇总梳理&#xff0c;文章会随着项目的开发推进不断更新。 这里是续文&#xff0c;因为CSDN单篇文章内容太大的话就会崩溃的&#xff0c;别问我怎么知道的&#xff0c;问就是血泪教训&#xff0c;辛辛苦苦写了一…

C语言 ——指针数组与数组指针

目录 一、二维数组 二、指针数组 &#xff08;1&#xff09;概念 &#xff08;2&#xff09;书写方式 &#xff08;3&#xff09;指针数组模拟二维数组 三、数组指针 &#xff08;1&#xff09;概念 &#xff08;2&#xff09;使用数组指针打印一维数组 &#xff08;3&a…

2023牛客暑期多校训练营7

Beautiful Sequence 贪心&#xff0c;二进制&#xff0c;构造 Cyperation 模拟 &#xff0c;数学 We Love Strings 分块&#xff0c;二进制枚举&#xff0c;二进制容斥dp Writing Books 签到 根据相邻两个异或值B&#xff0c;因为前小于等于后&#xff0c;故从高到低遍历B的每一…