springdoc-openapi使用

server/2024/9/22 23:59:27/

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 globalHeaderCustomizer() {//遍历所有操作(即接口)return (Operation operation, HandlerMethod handlerMethod) -> {//设置全局请求头参数setHeader(operation);return operation;};}private void setHeader(Operation operation){Parameter clusterIdParam = new Parameter().in(ParameterIn.HEADER.toString()).schema(new StringSchema()).name("clusterId").description("clusterId").required(false);Parameter tokenParam = new Parameter().in(ParameterIn.HEADER.toString()).schema(new StringSchema()).name("sessionid").description("sessionid").required(true);Parameter userIdParam = new Parameter().in(ParameterIn.HEADER.toString()).schema(new IntegerSchema()).name("userid").description("userid").required(true);Parameter useraccountParam = new Parameter().in(ParameterIn.HEADER.toString()).schema(new StringSchema()).name("useraccount").description("useraccount").required(true);Parameter userroleParam = new Parameter().in(ParameterIn.HEADER.toString()).schema(new StringSchema()).name("userrole").description("userrole").required(false);Parameter projectnameParam = new Parameter().in(ParameterIn.HEADER.toString()).schema(new StringSchema()).name("projectname").description("projectname").required(false);operation.addParametersItem(clusterIdParam);operation.addParametersItem(projectnameParam);operation.addParametersItem(tokenParam);operation.addParametersItem(useraccountParam);operation.addParametersItem(userIdParam);operation.addParametersItem(userroleParam);}@Beanpublic OpenApiCustomiser globalResponseCustomizer() {return openApi -> {openApi.getPaths().forEach((path, pathItem) -> {pathItem.readOperations().forEach(operation -> {ApiResponses responses = operation.getResponses();if(responses == null){responses = new ApiResponses();}// 添加全局响应addGlobalResponse(responses);// 将合并后的响应设置回操作operation.setResponses(responses);});});};}private void addGlobalResponse(ApiResponses responses){responses.addApiResponse("0",new ApiResponse().description("成功"));responses.addApiResponse("1",new ApiResponse().description("失败"));for (ServerStatus item : ServerStatus.values()) {responses.addApiResponse(String.valueOf(item.getErrorCode()),new ApiResponse().description(item.getMessage()));}}}

全局请求头参数设置参考文章:
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/server/15405.html

相关文章

Java 数据类型转换

Java 语言支持两种数据类型&#xff1a;基本数据类型和引用数据类型。 1. 基本数据类型 基本数据类型是直接存储在内存中的数据类型&#xff0c;包括&#xff1a; 整数类型&#xff1a;byte、short、int、long浮点数类型&#xff1a;float、double字符类型&#xff1a;char布…

JVM 引用的分类

引用计数算法和根搜索算法都需要通过判断引用的方式判断对象是否可回收。 JDK1.2 之前&#xff0c;Java 中引用的定义很传统&#xff1a;如果 reference 类型的数据存储的数值代表的是另一块内存的起始地址&#xff0c;就称这块内存代表一个引用。 在 JDK 1.2 之后&#xff0c;…

【virtuoso】 PDK

什么是PDK&#xff1f; PDK( Process Design Kit )&#xff0c;工程设计数据包&#xff0c;是芯片厂家foundary提供给IC设计公司的有关制造工艺的模型和EDA工具支持。是连接IC制造公司&#xff0c;IC设计公司的桥梁。 PDK包含内容&#xff1a; 器件模型 SPICE模型模型 与 测量误…

win c++使用lua环境配置 5.3.5版本

编译lua 下载lua源码&#xff0c;github仓库 使用vs编译源码&#xff0c;新建一个静态库项目(只会生成lib文件)&#xff0c;想要dll的话就新建dll项目&#xff08;有一个lib文件和dll文件&#xff09; 把lua源码下面的文件夹都是&#xff0c;复制到vs项目中 lib目录是我手动…

SSTV音频转图片

SSTV工具有很多&#xff0c;这里使用RX-SSTV慢扫描工具 下载安装 RX-SSTV解码软件 下载地址&#xff1a;https://www.qsl.net/on6mu/rxsstv.htm 一直点下一步&#xff0c;安装成功如下图: 虚拟声卡e2eSoft 由于SSTV工具是根据音频传递图片信息&#xff0c;正常解法需要一…

Esko Ukkonen: On-line Construction of Suffix Trees

Esko Ukkonen: On-line Construction of Suffix Trees 文章目录 Esko Ukkonen: On-line Construction of Suffix Trees一、后缀树的概念及应用【详见刘方州同学报告】1.1 字典树 Trie1.2 后缀树 Suffix Tree2 后缀树的应用 二、朴素后缀树构造方法及问题三、线性时间内后缀树在…

二刷大数据(三)- Flink1.17

目录 Flink概念与SparkStreaming区别分层API 工作流程部署模式**Local Mode****Standalone Mode****YARN Mode****Kubernetes Mode****Application Mode** 运行架构stand alone 核心概念算子链任务槽 窗口窗口**窗口的目的与作用****时间窗口&#xff08;Time Windows&#xff…

【继承和多态】

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、pandas是什么&#xff1f;二、使用步骤 1.引入库2.读入数据总结 前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 例如&#xff1a;…