Springboot集成SpringDoc接口文檔方式
前言
基于springboot3.0+jdk17+springdoc2.2實(shí)現(xiàn)。
一、創(chuàng)建springboot項(xiàng)目,引入springdoc依賴

二、編寫springdoc配置文件SpringDocConfig.java
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springdoc.core.utils.Constants;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
/**
* SpringDoc配置類
*
* @author TheTsing
*/
@Configuration
@ConditionalOnProperty(name = Constants.SPRINGDOC_ENABLED, matchIfMissing = true)
public class SpringDocConfig {
@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.info(new Info().title("API Documentation").version("snapshot"))
.schemaRequirement(HttpHeaders.AUTHORIZATION,
new SecurityScheme()
// 普通 Token
//.name(HttpHeaders.AUTHORIZATION)
//.type(SecurityScheme.Type.APIKEY)
//.in(SecurityScheme.In.HEADER)
// Bearer Token
.name(HttpHeaders.AUTHORIZATION)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer"))
.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION));
}
@Bean
public GlobalOpenApiCustomizer globalOpenApiCustomizer() {
return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream()).forEach(operation -> {
operation.getResponses().addApiResponse(String.valueOf(HttpStatus.BAD_REQUEST.value()),
new ApiResponse().description("客戶端請求存在語法錯(cuò)誤或業(yè)務(wù)異常").content(new Content().addMediaType(MediaType.TEXT_PLAIN_VALUE, new io.swagger.v3.oas.models.media.MediaType().example(HttpStatus.BAD_REQUEST.getReasonPhrase()))));
operation.getResponses().addApiResponse(String.valueOf(HttpStatus.UNAUTHORIZED.value()),
new ApiResponse().description("認(rèn)證失敗").content(new Content().addMediaType(MediaType.TEXT_PLAIN_VALUE, new io.swagger.v3.oas.models.media.MediaType().example(HttpStatus.UNAUTHORIZED.getReasonPhrase()))));
operation.getResponses().addApiResponse(String.valueOf(HttpStatus.FORBIDDEN.value()),
new ApiResponse().description("沒有權(quán)限").content(new Content().addMediaType(MediaType.TEXT_PLAIN_VALUE, new io.swagger.v3.oas.models.media.MediaType().example(HttpStatus.FORBIDDEN.getReasonPhrase()))));
operation.getResponses().addApiResponse(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),
new ApiResponse().description("服務(wù)器內(nèi)部錯(cuò)誤").content(new Content().addMediaType(MediaType.TEXT_PLAIN_VALUE, new io.swagger.v3.oas.models.media.MediaType().example(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()))));
});
}
@Bean
@Primary
public SwaggerUiConfigProperties swaggerUiConfig() {
SwaggerUiConfigProperties config = new SwaggerUiConfigProperties();
config.setPersistAuthorization(true);
config.setFilter("true");
config.setDefaultModelsExpandDepth(-1);
config.setDefaultModelExpandDepth(10);
config.setDisplayRequestDuration(true);
config.setDocExpansion("none");
config.setShowCommonExtensions(true);
return config;
}
}
三、啟動(dòng)項(xiàng)目訪問文檔
http://localhost:8080/swagger-ui/index.html

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中@ConditionalOnBean注解的具體使用
本文主要介紹了SpringBoot中@ConditionalOnBean注解的具體使用,用于根據(jù)是否存在指定Bean動(dòng)態(tài)注冊Bean,支持類型、名稱、注解及搜索范圍控制,常見于按需加載、自動(dòng)配置和可選依賴場景,與@ConditionalOnMissingBean形成條件對立2025-06-06
Java實(shí)現(xiàn)微信掃碼登入的實(shí)例代碼
這篇文章主要介紹了java實(shí)現(xiàn)微信掃碼登入功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
springboot項(xiàng)目中后端接收前端傳參的方法示例詳解
這篇文章主要介紹了springboot項(xiàng)目中一些后端接收前端傳參的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
解析spring-boot-starter-parent簡介
本文通過代碼的形式給大家介紹了spring-boot-starter-parent的基礎(chǔ)知識(shí),需要的朋友可以參考下2018-09-09
Netty分布式pipeline管道創(chuàng)建方法跟蹤解析
這篇文章主要為大家介紹了Netty分布式pipeline管道創(chuàng)建方法跟蹤解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
開放封閉原則_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了開放封閉原則,開放-封閉原則是面向?qū)ο笤O(shè)計(jì)的核心所在,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
SpringBoot項(xiàng)目接入MQTT的詳細(xì)指南
MQTT是一種輕量級的消息傳輸協(xié)議,特別適用于物聯(lián)網(wǎng)(IoT)場景,具有低帶寬、高延遲網(wǎng)絡(luò)環(huán)境下的優(yōu)勢,SpringBoot作為流行的 Java開發(fā)框架,能夠方便地與MQTT集成,實(shí)現(xiàn)高效的消息通信,本文將詳細(xì)介紹如何在SpringBoot項(xiàng)目中接入MQTT,需要的朋友可以參考下2025-03-03
Java純代碼實(shí)現(xiàn)導(dǎo)出PDF功能
在項(xiàng)目開發(fā)中,產(chǎn)品的需求越來越奇葩啦,開始文件下載都是下載為excel的,做著做著需求竟然變了,要求能導(dǎo)出pdf,本文就來和大家分享一下Java實(shí)現(xiàn)導(dǎo)出PDF的常用方法吧2023-07-07

