首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >@BeanParam在使用@Parameter进行注释时没有记录

@BeanParam在使用@Parameter进行注释时没有记录
EN

Stack Overflow用户
提问于 2020-02-26 16:24:46
回答 1查看 1.1K关注 0票数 2

我正在尝试使用swagger-maven-plugin来记录我的api。

当我用@Parameter注释路由param时,只要它没有用@BeanParam注释,openapi生成的文件中就有很好的文档。

正如在swagger核心文档中所指出的,

@PathParam可以代替JAX参数注释,也可以与JAX-RS参数注释一起使用(@PathParam、@QueryParam、@HeaderParam、@FormParam和@BeanParam)。默认情况下,swagger-core / swagger-jaxrs2 2扫描这些注释时,@Parameter允许为参数定义更多的详细信息。

如何在openapi文件中记录我的@BeanParam参数?

这是我的pom.xml

代码语言:javascript
运行
复制
<dependencies>
    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-jaxrs2</artifactId>
        <version>2.1.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <outputFileName>openapi</outputFileName>
                <outputPath>target/doc</outputPath>
                <outputFormat>YAML</outputFormat>
                <resourcePackages>...</resourcePackages>
                <readAllResources>false</readAllResources>
            </configuration>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>resolve</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

在这里,我的带注释的api:

代码语言:javascript
运行
复制
@GET
@Path("/authorize")
@Operation(summary = "Summary", description = "Description")
Response authorize(
        // Parameter doesn't show up in generated documentation
        @Parameter(
                description = "Request",
                name = "request",
                required = true
        )
        @BeanParam Oauth2AuthorizationRequest request,
        // Parameter shows up correctly in generated documentation
        @Parameter(
                description = "Session id",
                name = "sessionId",
                required = true
        )
        @CookieParam("sessionId") String sessionId
);

生成的文件是:

代码语言:javascript
运行
复制
openapi: 3.0.1
paths:
  /auth/oauth2/authorize:
    get:
      summary: Summary
      description: Description
      operationId: authorize
      parameters:
      - name: sessionId
        in: cookie
        description: Session id
        required: true
        schema:
          type: string
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-27 20:14:30

经过一些试验,我设法在openapi文件中记录了我的@BeanParam。注释必须放在带有注释的类的@BeanParam中,如下所示:

代码语言:javascript
运行
复制
public class Oauth2AuthorizationRequest {
    // Use the @Parameter annotation to document the attribute.
    @HeaderParam("Authorization")
    @Parameter(description = "Authorization header")
    String authorization;

    // If the attribute is a @FormParam, use the @Schema annotation.
    @FormParam("client_id")
    @Schema(description = "The id of the client")
    String client_id;

    // If the attribute is a @FormParam and is required, 
    // use the @Schema annotation for the description
    // and the @Parameter one to set it as required.
    @FormParam("grant_type")
    @Schema(description = "Should be either \"credentials\" or \"password\"")
    @Parameter(required = true)
    String grant_type;
}

并将端点简化为:

代码语言:javascript
运行
复制
@GET
@Path("/authorize")
@Operation(summary = "Summary", description = "Description")
Response authorize(
    // No more annotation on the @BeanParam
    @BeanParam Oauth2AuthorizationRequest request,
    @Parameter(
        description = "Session id",
        name = "sessionId",
        required = true
    )
    @CookieParam("sessionId") String sessionId
);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60418319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档