【问题标题】:swagger-maven-plugin is not generating @ApiResponse schemesswagger-maven-plugin 没有生成@ApiResponse 方案
【发布时间】:2021-05-31 06:43:08
【问题描述】:

我正在使用 swagger-maven-plugin 在编译时生成 YAML 和 JSON 文件,如下所示:

   <plugin>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>2.1.7</version>
    <configuration>
      <outputFileName>openapi</outputFileName>
      <outputPath>${project.build.directory}/openapi</outputPath>
      <outputFormat>JSONANDYAML</outputFormat>
      <resourcePackages>
        <package>mypackage.rest</package>
      </resourcePackages>
      <prettyPrint>true</prettyPrint>
    </configuration>
    <executions>
      <execution>
        <phase>compile</phase>
        <goals>
          <goal>resolve</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

两个文件都在生成,但“响应”是默认的:

"/transactions/{id}" : {
  "get" : {
    "operationId" : "getSingleTransaction",
    "parameters" : [ {
      "name" : "id",
      "in" : "path",
      "required" : true,
      "schema" : {
        "type" : "string"
      }
    } ],
    "responses" : {
      "default" : {
        "description" : "default response",
        "content" : {
          "application/vnd.oracle.resource+json;type=singular" : { }
        }
      }
    }
  }
},

但是,在 Java 类文件中,它使用 @ApiResponse 关键字进行注释:

@GET
@Operation(summary = "Get collection of Transactions for the given transactionId",
        description = "Get collection of Transactions for the given transactionId")
@APIResponse(description = "A list of TransactionDto",
        content = @Content(mediaType = APPLICATION_JSON,
                schema = @Schema(type = ARRAY, implementation = TransactionDto.class)))
@Consumes(APPLICATION_JSON)
@Produces(COLLECTION)
public Response getApprovalTasksForTransactionId(
        @PathParam("transactionId") TransactionId transactionId) {

    ...
}

请注意,“ProductsDto”是 ApiResponse 的一部分,但 YAML 定义将其设为默认值。

为了正确映射响应和响应类型方案,我可能缺少什么?

【问题讨论】:

    标签: maven swagger openapi


    【解决方案1】:

    我发现我在 java 文件中的注释是从以下位置导入的:

    import org.eclipse.microprofile.openapi.annotations
    

    发现 swagger-maven-plugin 需要以下注解 :

    import io.swagger.v3.oas.annotations
    

    【讨论】: