Springdoc-Openapi不包括全局标头或API-DOC中的响应

发布于 2025-01-21 21:15:17 字数 2113 浏览 0 评论 0原文

我们将Springfox移植到SpringDoc,并且存在获取全局参数和默认响应的问题,以显示在 /V3 /API-DOCS响应中。

它们在Swagger UI中表现良好,但在JSON中没有从 /v3 /api-docs返回。我们正在从这些API文档中生成代码。

能够使标头在“组件”部分下显示,但是标题和响应并未像SpringFox一样在JSON API-DOCS输出中的每个端点下显示。

@Bean
public GroupedOpenApi groupedOpenApi() {
    final OperationCustomizer globalHeader = (operation, handlerMethod) -> {
        operation.addParametersItem(new HeaderParameter()
            .$ref("#/components/parameters/testheader"));
        return operation;
    };

    return GroupedOpenApi.builder()
        .group("default").pathsToMatch("/**")
        .addOperationCustomizer(globalHeader)
        .addOpenApiCustomiser(getResponseMessages()).build();
}

@Bean
public OpenAPI openApi() {
    return new OpenAPI()
        .info(new Info().title("testing").description("testing").termsOfService("")
            .license(new License().name("").url("")).version("1.0"))
        .components(new Components()
            .addParameters(
                "testheader",
                new Parameter()
                    .in(ParameterIn.HEADER.toString())
                    .name("testheader").description("test header")
                    .required(true).example("sdfdsafsf").schema(new StringSchema())));
}

private OpenApiCustomiser getResponseMessages() {
    return openApi -> {
        openApi.getPaths().values().forEach(pathItem ->
            pathItem.readOperations().forEach(operation -> {
                ApiResponses apiResponses = operation.getResponses();
                apiResponses.addApiResponse(
                    String.valueOf(HttpStatus.BAD_REQUEST.value()),
                    new ApiResponse().description("Bad request"));
                apiResponses.addApiResponse(
                    String.valueOf(HttpStatus.UNAUTHORIZED.value()),
                    new ApiResponse().description("Not authorized"));
            }));
    };
}

关于我缺少什么的想法?谢谢。

这是一个小型春季启动应用程序,证明了问题: https://github.com/ens121/swaggertest

We are porting springfox to springdoc and are having issues getting the global parameters and default responses to show up in the /v3/api-docs response.

They show up fine in the Swagger UI but not in the json returned from /v3/api-docs. We are generating code from these API docs.

Was able to get the headers to show up under the components section but the headers and responses do not show up under each endpoint in the json api-docs output like it did with springfox.

@Bean
public GroupedOpenApi groupedOpenApi() {
    final OperationCustomizer globalHeader = (operation, handlerMethod) -> {
        operation.addParametersItem(new HeaderParameter()
            .$ref("#/components/parameters/testheader"));
        return operation;
    };

    return GroupedOpenApi.builder()
        .group("default").pathsToMatch("/**")
        .addOperationCustomizer(globalHeader)
        .addOpenApiCustomiser(getResponseMessages()).build();
}

@Bean
public OpenAPI openApi() {
    return new OpenAPI()
        .info(new Info().title("testing").description("testing").termsOfService("")
            .license(new License().name("").url("")).version("1.0"))
        .components(new Components()
            .addParameters(
                "testheader",
                new Parameter()
                    .in(ParameterIn.HEADER.toString())
                    .name("testheader").description("test header")
                    .required(true).example("sdfdsafsf").schema(new StringSchema())));
}

private OpenApiCustomiser getResponseMessages() {
    return openApi -> {
        openApi.getPaths().values().forEach(pathItem ->
            pathItem.readOperations().forEach(operation -> {
                ApiResponses apiResponses = operation.getResponses();
                apiResponses.addApiResponse(
                    String.valueOf(HttpStatus.BAD_REQUEST.value()),
                    new ApiResponse().description("Bad request"));
                apiResponses.addApiResponse(
                    String.valueOf(HttpStatus.UNAUTHORIZED.value()),
                    new ApiResponse().description("Not authorized"));
            }));
    };
}

Any ideas on what I'm missing? Thank you.

Here is a small spring boot application that demonstrates the issue:
https://github.com/ens121/swaggertest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2025-01-28 21:15:17

可以使用@openapidefinition注释:

@OpenAPIDefinition(info = @Info(title = "${openapi.info.title}", version = "${openapi.info.version}",
        description = "${openapi.info.description}", contact = @Contact(name = "${openapi.info.contact.name}", email = "${openapi.info.contact.email}"),
        license = @License(url = "${openapi.info.license.url}", name = "${openapi.info.license.name}")))
@SpringBootApplication

带有application.properties

# OPENAPI INFOS displayed with the @OpenAPIDefinition annotation
openapi.info.title=App Title
openapi.info.version=2.12.5
openapi.info.description=Application Description
openapi.info.contact.name=Principal Direction, Subdivision Name, Acronym
[email protected]
openapi.info.license.name=CorpName
openapi.info.license.url=http://www.mycorp.com

server.servlet.context-path=/context
server.port=27743

哪个产生:

The global API definitions can be set with the @OpenAPIDefinition annotation :

@OpenAPIDefinition(info = @Info(title = "${openapi.info.title}", version = "${openapi.info.version}",
        description = "${openapi.info.description}", contact = @Contact(name = "${openapi.info.contact.name}", email = "${openapi.info.contact.email}"),
        license = @License(url = "${openapi.info.license.url}", name = "${openapi.info.license.name}")))
@SpringBootApplication

with the values provided in your application.properties :

# OPENAPI INFOS displayed with the @OpenAPIDefinition annotation
openapi.info.title=App Title
openapi.info.version=2.12.5
openapi.info.description=Application Description
openapi.info.contact.name=Principal Direction, Subdivision Name, Acronym
[email protected]
openapi.info.license.name=CorpName
openapi.info.license.url=http://www.mycorp.com

server.servlet.context-path=/context
server.port=27743

which yields :

swagger output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文