springdoc-openapi使用

一、引入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("制品中心 后台服务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() {
        //设置全局请求头参数
        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;
        };
    }

}

全局请求头参数设置参考文章:
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  = "设置制品库权限")
    @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 请求参数
     *
     * @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库路径");
        }
    }

}

五、配置文件新增内容

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
在这里插入图片描述
展示接口页面表示成功

相关推荐

  1. SpringIoC原理

    2024-04-21 20:34:04       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-21 20:34:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-21 20:34:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-21 20:34:04       82 阅读
  4. Python语言-面向对象

    2024-04-21 20:34:04       91 阅读

热门阅读

  1. AMEYA360:兆易创新推出GD32L235系列低功耗MCU新品

    2024-04-21 20:34:04       42 阅读
  2. 基于httpd和lvs的dr模式简单测试

    2024-04-21 20:34:04       40 阅读
  3. 2024-4-17-ARM作业

    2024-04-21 20:34:04       29 阅读
  4. 第十五届蓝桥杯C/C++B组题解

    2024-04-21 20:34:04       30 阅读
  5. Python语言零基础入门——组合数据类型(一)

    2024-04-21 20:34:04       94 阅读
  6. MATLAB初学者入门(3)—— 优化模型求解

    2024-04-21 20:34:04       39 阅读
  7. TPCC MySQL

    2024-04-21 20:34:04       38 阅读