欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springdoc?openapi使用解決方案

 更新時間:2024年04月22日 09:57:49   作者:qq_44209563  
SpringDoc注解的使用,它是基于OpenAPI?3和Swagger?3的現(xiàn)代化解決方案,相較于舊版的Swagger2即SpringFox,SpringDoc提供了更簡潔、更直觀的注解方式,這篇文章主要介紹了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è)置參考文章:

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 {
    @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)文章

最新評論