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)文章
Spring Bean生命周期之Bean元信息的配置與解析階段詳解
這篇文章主要為大家詳細(xì)介紹了Spring Bean生命周期之Bean元信息的配置與解析階段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
java實現(xiàn)切圖并且判斷圖片是不是純色/彩色圖片
本篇文章主要介紹了java實現(xiàn)切圖并且判斷圖片是否是純色/彩色圖片,具有一定的參考價值,有興趣的可以了解一下2017-08-08
Java通過wait()和notifyAll()方法實現(xiàn)線程間通信
這篇文章主要為大家詳細(xì)介紹了Java通過wait()和notifyAll()方法實現(xiàn)線程間通信的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
SpringBoot實現(xiàn)文件上傳與下載功能的詳細(xì)教程
在日常開發(fā)中,文件上傳和下載是非常常見的功能場景,比如用戶頭像上傳、文檔下載、附件管理等,本文將基于?SpringBoot?框架,實現(xiàn)一套簡潔、通用、可直接上線使用的文件上傳與下載接口,需要的朋友可以參考下2026-05-05
Nacos多個實例的服務(wù)調(diào)用失敗問題及解決
在微服務(wù)開發(fā)中,Nacos上出現(xiàn)多個同一服務(wù)實例導(dǎo)致OpenFeign調(diào)用失敗,因為OpenFeign使用SpringCloudLoadbalancer,而SpringCloudLoadbalancer的底層默認(rèn)負(fù)載均衡策略不支持Nacos,通過將負(fù)載均衡策略設(shè)置為Nacos,可以解決調(diào)用失敗的問題2026-02-02

