Spring Boot 如何使用openapi-generator-maven插件将requestHeader添加到生成的接口

plupiseo  于 2022-11-23  发布在  Spring
关注(0)|答案(1)|浏览(274)

我正在使用openapi-generator-maven-plugin为springboot应用程序生成服务器代码。
然而,我无法找到一种简单的方法来在接口中生成@RequestHeader("header")
以下是插件配置:

<plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <!-- RELEASE_VERSION -->
                <version>5.1.0</version>
                <!-- /RELEASE_VERSION -->
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/spec/api.yaml</inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>my.api</apiPackage>
                            <modelPackage>my.models</modelPackage>
                            <configOptions>
                                <delegatePattern>false</delegatePattern>
                                <interfaceOnly>true</interfaceOnly>
                                <useTags>true</useTags>
                <useOptional>true</useOptional>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

API规格:

get:
      tags:
        - template
      summary: find template by template name
      description: returns single template
      operationId: getTemplateByName
      parameters:
        - name: bucket
          in: path
          description: location
          required: true
          schema:
            type: string
        - name: templateName
          in: path
          description: the name of the template
          required: true
          schema:
            type: string
      responses:
        200:
          description: successful operation
          content:
            multipart/mixed:
              schema:
                type: object
            application/json:
              schema:
                $ref: '#/components/schemas/Template'
        400:
          description: Invalid input
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        404:
          description: template not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'

生成界面:

@ApiOperation(value = "find template by template name", nickname = "getTemplateByName", notes = "returns single template", response = Object.class, tags={ "template", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "successful operation", response = Object.class),
        @ApiResponse(code = 400, message = "Invalid input", response = Error.class),
        @ApiResponse(code = 404, message = "template not found", response = Error.class) })
    @ApiImplicitParams({
    })
    @GetMapping(
        value = "/bu/{bucket}/templates/{templateName}",
        produces = { "multipart/mixed", "application/json" }
    )
    default ResponseEntity<Object> getTemplateByName(@ApiParam(value = "location",required=true) @PathVariable("bucket") String bucket,@ApiParam(value = "the name of the template",required=true) @PathVariable("templateName") String templateName) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

如何将@RequestHeader添加到接口中?或者是否有一种方法可以在接口中生成httprequest?

qnyhuwrf

qnyhuwrf1#

正如在swagger Docs中提到的,可以使用header完成此操作,如下所示

paths:
  /ping:
    get:
      summary: Checks if the server is alive
      parameters:
        - in: header
          name: X-Request-ID
          schema:
            type: string
            format: uuid
          required: true

以类似的方式,您可以定义自定义响应标头。标头参数可以是基元、数组和对象。数组和对象使用简单样式进行序列化。
注意:不允许使用名为Accept、Content-Type和Authorization的信头参数。若要描述这些信头,请使用Map的OpenAPI保留字:

相关问题