springdoc?openapi使用解決方案
什么是SpringDoc
SpringDoc注解的使用,它是基于OpenAPI 3和Swagger 3的現(xiàn)代化解決方案,相較于舊版的Swagger2(SpringFox),SpringDoc提供了更簡潔、更直觀的注解方式。
一、引入pom
<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.5.12</version> </dependency>
二、新增配置類OpenApiConfig
@Configuration public class OpenApiConfig { @Bean public OpenAPI springShopOpenAPI() { OpenAPI openAPI = new OpenAPI() .info(new Info().title("制品中心 后臺服務(wù)API接口文檔") .description("restful 風(fēng)格接口") .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; } @Bean public OperationCustomizer customGlobalHeaders() { //設(shè)置全局請求頭參數(shù) 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; }; } }
全局請求頭參數(shù)設(shè)置參考文章:
三、Controller層示例
@Controller @RequestMapping("/test") @Tag(name = "測試接口") @Validated public class TestController { @Autowired private ArtifactService artifactService; @PostMapping("/v1/test") @Operation(summary = "設(shè)置制品庫權(quán)限") @NoPermission public 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") @NoPermission public Result<Void> add(String name, String typeId) { return null; } /** * 查詢generic制品庫下所有文件列表 * * @param quest 請求參數(shù) * * @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("獲取當(dāng)前generic制品庫包含的制品成功", vo); } else { return Result.failed("請求路徑非generic庫路徑"); } } }
四、配置文件新增內(nèi)容
application.yaml
springdoc: swagger-ui: # swagger-ui地址 path: /swagger-ui/index.html enabled: true # 修復(fù)Failed to load remote configuration. # To configure, the path of a custom OpenAPI file . Will be ignored if urls is used url: /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
展示接口頁面表示成功
到此這篇關(guān)于springdoc openapi使用的文章就介紹到這了,更多相關(guān)springdoc openapi使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
@NotEmpty、@NotBlank、@NotNull的區(qū)別
這篇文章主要介紹了@NotEmpty、@NotBlank、@NotNull的區(qū)別,需要的朋友可以參考下2016-09-09解決Springboot項目打包后的頁面丟失問題(thymeleaf報錯)
這篇文章主要介紹了解決Springboot項目打包后的頁面丟失問題(thymeleaf報錯),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot構(gòu)造器注入循環(huán)依賴及解決方案
這篇文章主要介紹了SpringBoot構(gòu)造器注入循環(huán)依賴及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Springboot?接口需要接收參數(shù)類型是數(shù)組問題
這篇文章主要介紹了Springboot?接口需要接收參數(shù)類型是數(shù)組問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01springboot的LogbackLoggingSystem配置加載流程解析
這篇文章主要介紹了springboot的LogbackLoggingSystem配置加載流程源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11通過web控制當(dāng)前的SpringBoot程序重新啟動
本文主要給大家介紹了如何通過web控制當(dāng)前的SpringBoot程序重新啟動,文章給出了詳細(xì)的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-11-11