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 風格接口") .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("獲取當前generic制品庫包含的制品成功", vo); } else { return Result.failed("請求路徑非generic庫路徑"); } } }
四、配置文件新增內(nèi)容
application.yaml
springdoc: swagger-ui: # swagger-ui地址 path: /swagger-ui/index.html enabled: true # 修復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)文章
Java中使用RediSearch實現(xiàn)高效的數(shù)據(jù)檢索功能
RediSearch是一款構(gòu)建在Redis上的搜索引擎,它為Redis數(shù)據(jù)庫提供了全文搜索、排序、過濾和聚合等高級查詢功能,本文將介紹如何在Java應(yīng)用中集成并使用RediSearch,以實現(xiàn)高效的數(shù)據(jù)檢索功能,感興趣的朋友跟著小編一起來看看吧2024-05-05Spring Data Jpa Mysql使用utf8mb4編碼的示例代碼
這篇文章主要介紹了Spring Data Jpa Mysql使用utf8mb4編碼的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11使用ehcache三步搞定springboot緩存的方法示例
本次內(nèi)容主要介紹基于Ehcache 3.0來快速實現(xiàn)Spring Boot應(yīng)用程序的數(shù)據(jù)緩存功能。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04