springdoc-openapi使用

devtools/2024/9/22 17:30:36/

springdoc-openapi使用

  • 一、引入pom
  • 二、新增配置类OpenApiConfig
  • 四、Controller层示例
  • 五、配置文件新增内容
  • 六、验证

一、引入pom

        <dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-ui</artifactId><version>1.5.12</version></dependency>

二、新增配置类OpenApiConfig

@Configuration
public class OpenApiConfig {@Beanpublic OpenAPI springShopOpenAPI() {OpenAPI openAPI = new OpenAPI().info(new Info().title("制品中心 后台服务API接口文档").description("restful 风格接口").version("v0.0.1").license(new License().name("Apache 2.0").url("http://springdoc.org"))).externalDocs(new ExternalDocumentation().description("SpringShop Wiki Documentation").url("https://springshop.wiki.github.org/docs"));return openAPI;}@Beanpublic OperationCustomizer customGlobalHeaders() {//设置全局请求头参数return (Operation operation, HandlerMethod handlerMethod) -> {Parameter tokenParam = new Parameter().in(ParameterIn.HEADER.toString()).schema(new StringSchema()).name("sessionid").description("sessionid").required(true);operation.addParametersItem(tokenParam);return operation;};}}

全局请求头参数设置参考文章:
https://stackoverflow.com/questions/63671676/springdoc-openapi-ui-add-jwt-header-parameter-to-generated-swagger

四、Controller层示例

@Controller
@RequestMapping("/test")
@Tag(name = "测试接口")
@Validated
public class TestController {@Autowiredprivate ArtifactService artifactService;@PostMapping("/v1/test")@Operation(summary  = "设置制品库权限")@NoPermissionpublic Result<Void> addArtifactPermission(@Validated @RequestBody AssetAuthDataDTO assetAuthData, @RequestHeader(value = "adminaction", defaultValue = "false") boolean adminAction) {return null;}@Operation(summary = "添加", description = "添加描述",security = { @SecurityRequirement(name = "sessionid")},responses = {@ApiResponse(description = "返回信息", content = @Content(mediaType = "application/json")),@ApiResponse(responseCode = "400", description = "返回400时候错误的原因")})@Parameters({@Parameter(name = "name", description = "名字", required = true),@Parameter(name = "typeId", description = "类型ID", required = true)})@PutMapping("add")@NoPermissionpublic Result<Void> add(String name, String typeId) {return null;}/*** 查询generic制品库下所有文件列表** @param quest 请求参数** @return** @author wangsb9* @data: 2023/4/6 14:54*/@ApiOperation(value = "查询generic制品库下所有文件列表", httpMethod = "GET")@GetMapping("/v1/generic/item/list")@ResponseBody@RepoKeyPermission(permission = "read", param = "quest.repoKey")public Result<GenericItemListVO> getGenericItemList(GetGenericItemListQuest quest) {if (ArtifactTypes.GENERIC.equalsIgnoreCase(BusinessUtils.getArtifactTypeFromRepoKey(quest.getRepoKey()))) {GenericItemListVO vo = artifactService.geGenericItemList(quest);return Result.success("获取当前generic制品库包含的制品成功", vo);} else {return Result.failed("请求路径非generic库路径");}}}

五、配置文件新增内容

application.yaml

springdoc:swagger-ui:# swagger-ui地址path: /swagger-ui/index.htmlenabled: true# 修复Failed to load remote configuration.
#    To configure, the path of a custom OpenAPI file . Will be ignored if urls is usedurl: /springdoc/api-docs
#  For custom path of the OpenAPI documentation in Json format.api-docs:path: /springdoc/api-docs
#  packages-to-scan: com.srdcloud.artifact.controller

六、验证

启动项目后访问地址http://<serviceIp>:<port>/swagger-ui/index.html
在这里插入图片描述
展示接口页面表示成功


http://www.ppmy.cn/devtools/7404.html

相关文章

网络原理-UDP和TCP

在传输层中有两个非常重要的协议&#xff0c;UDP和TCP&#xff0c;现在就来研究一下这两个协议。 UDP 报文格式 我们观察可以发现&#xff0c;里面UDP报文长度为2个字节&#xff0c;那么是多少呢&#xff1f;我们需要快速反应如下固定字节数据类型的取值范围&#xff1a; 字…

GPT 浅析

GPT 浅析 文章目录 GPT 浅析GPT 1无监督预训练有监督微调任务相关的输入变换 GPT2GPT3 GPT 1 在模型架构上&#xff0c;GPT-1基于Transformer构造&#xff0c;这是因为与其他卷积神经网 络或者循环神经网络相比&#xff0c;Transformer提供了效率更高的方法来处理文本 中的长期…

Linux Makefile

程序的编译和链接 使用C、C编写可执行程序&#xff0c;首先要把源文件编译成中间代码文件&#xff0c;Linux下是 .o 文件&#xff0c;即 Object File&#xff0c;这个动作叫做编译&#xff08;compile&#xff09;。然后再把大量的Object File合成执行文件&#xff0c;这个动作…

ansible执行mysql脚本

目录 概述实践环境要求ansible yml脚本命令离线包 概述 ansible执行mysql脚本 实践 官网文档 环境要求 环境需要安装以下内容: 1.mysql客户端(安装了mysql即会有)2.安装MySQL-python (Python 2.X) 详细插件安装链接 ansible yml脚本 关键代码如下&#xff1a; # 剧本…

数据结构_时间复杂度

✨✨所属专栏&#xff1a;数据结构✨✨ ✨✨作者主页&#xff1a;嶔某✨✨ 什么是时间复杂度&#xff1f; 时间复杂度的定义&#xff1a;在计算机科学中&#xff0c;算法的时间复杂度是一个函数&#xff0c;它定量描述了该算法的运行时间。一个算法执行所耗费的时间&#xff0…

C语言中, 文件包含处理,#include< > 与 #include ““的区别

文件包含处理 指一个源文件可以将另外一个文件的全部内容包含进来 &#xff23;语言提供了#include命令用来实现文件包含的操作 #include< > 与 #include ""的区别 <> 表示系统直接按系统指定的目录检索 "" 表示系统先在 "" 指定…

Oracle——领先的企业级数据库解决方案

一、WHAT IS ORACLWE&#xff1a; ORACLE 数据库系统是美国 ORACLE 公司&#xff08;甲骨文&#xff09;提供的以分布式数据库为核心的一组软件产品&#xff0c;是目前最流行的客户/服务器(CLIENT/SERVER)或B/S 体系结构的数据库之一&#xff0c;ORACLE 通常应用于大型系统的数…

PHP命令执行漏洞CVE-2024-1874复现

CVE-2024-1874 PHP命令执行漏洞 影响版本 Affected versions < 8.1.28 < 8.2.18 < 8.3.5 Patched versions 8.1.28 8.2.18 8.3.6 POC 创建一个文件test.php <?php $descriptorspec [STDIN, STDOUT, STDOUT]; $proc proc_open(["test.bat", "\&…