springdoc-openapi使用

news/2024/9/23 7:01:53/

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/news/1428574.html

相关文章

svn使用(上传自己的项目到svn上)

安卓开发工具版本 创建项目后&#xff0c;首先在.gitgnore文件里面加入你要过滤的文件路径 然后点击VCS——》share Project&#xff0c;然后下一步选择一个svn路径&#xff0c;点击确定后。然后将代码提交。

搜索引擎中的倒排索引是什么

在搜索引擎领域&#xff0c;倒排索引是一种核心数据结构&#xff0c;它让搜索引擎能够以极高的效率找到包含用户查询关键词的所有网页。为了理解倒排索引的工作原理&#xff0c;我们可以将其与一种更直观、生活化的例子相比较&#xff1a;书店里的索引卡片系统。 假设你是一位…

掌握Node Version Manager(nvm):跨平台Node.js版本管理

&#x1f31f; 前言 欢迎来到我的技术小宇宙&#xff01;&#x1f30c; 这里不仅是我记录技术点滴的后花园&#xff0c;也是我分享学习心得和项目经验的乐园。&#x1f4da; 无论你是技术小白还是资深大牛&#xff0c;这里总有一些内容能触动你的好奇心。&#x1f50d; &#x…

网络协议——IS-IS协议详解

1. IS-IS是什么 IS-IS是一种基于链路状态并使用最短路径优先算法进行路由计算的一种IGP协议。IS-IS属于内部网关协议&#xff0c;用于自治系统内部。IS-IS是一种链路状态协议&#xff0c;使用最短路径优先算法进行路由计算。 2. 应用场景&#xff08;园区网和骨干网&#xff0…

学习笔记2024/4/17

共享代理&#xff0c;打开允许局域网 打开clash的allow LAN 查看mac的以太网的ip地址&#xff08;mac在局域网中的ip&#xff09; 修改下面命令中的ip&#xff0c;粘贴到服务器的终端中即可 终端是否代理 curl ipinfo.ioexport https_proxyhttp://10.15.223.121:7890; expor…

vite+vue3+antDesignVue 记录-持续记录

记录学习过程 持续补充 每天的学习点滴 开始时间2024-04-12 1&#xff0c;报错记录 &#xff08;1&#xff09;env.d.ts文件 解决方法&#xff1a; 在env.d.ts文件中添加以下代码&#xff08;可以看一下B站尚硅谷的讲解视频&#xff09; declare module *.vue {import { Defi…

超越GPT-4V,苹果多模态大模型上新,神经形态计算加速MLLM(二)

上文介绍基于MINOnets神经网络架构加速多模态大模型的策略&#xff0c;本文将以Spinnaker2多核神经网络芯片EGRU架构为起点&#xff0c;覆盖存内计算架构&#xff0c;介绍新型计算架构在加速大模型推理的作用。SpiNNaker 2是一个设计用于大规模异步处理的多核神经形态芯片&…

MongoDB的CURD(增删改查操作)

读者大大们好呀&#xff01;&#xff01;!☀️☀️☀️ &#x1f525; 欢迎来到我的博客 &#x1f440;期待大大的关注哦❗️❗️❗️ &#x1f680;欢迎收看我的主页文章➡️寻至善的主页 ✈️如果喜欢这篇文章的话 &#x1f64f;大大们可以动动发财的小手&#x1f449;&#…