swagger 使用OpenAPI生成不返回ResponseEntity的Java API

wfsdck30  于 2022-11-06  发布在  Java
关注(0)|答案(1)|浏览(275)

我希望使用OpenAPI 3.0生成一个Java API,但不希望任何返回类型都是ResponseEntity。
我有一个小的.yaml文件,它生成的API看起来像这样(为了简洁起见,我删除了很多注解):

@GetMapping(value = "/student", produces = { "application/json" })
ResponseEntity<List<Student>> getAllStudents();

这非常简单,但我的雇主需要一个这样的API:

@GetMapping(value = "/student")
List<Student> getAllStudents();

问题是,我找不到一个配置选项来使代码生成器无需ResponseEntities。
是否有任何方法可以将OpenAPI生成器配置为不使用ResponseEntities?

详细数据:

我使用Maven进行构建,并在pom文件中配置代码生成,如下所示:

<build>
  <plugins>
    <plugin>
      <groupId>org.openapitools</groupId>
      <artifactId>openapi-generator-maven-plugin</artifactId>
      <!-- RELEASE_VERSION -->
      <version>5.0.0-SNAPSHOT</version>
      <!-- /RELEASE_VERSION -->
      <executions>
        <execution>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <!--See https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin-->
            <inputSpec>${project.basedir}/OpenApi.yaml</inputSpec>
            <generatorName>spring</generatorName>
            <groupId>com.dummy.example</groupId>
            <artifactId>dummy</artifactId>
            <artifactVersion>2.0</artifactVersion>
            <library>spring-boot</library>
            <packageName>com.neptunedreams.configuration</packageName>
            <apiPackage>com.neptunedreams.api</apiPackage>
            <invokerPackage>com.neptunedreams</invokerPackage>
            <modelPackage>com.neptunedreams.model</modelPackage>
            <configOptions>
              <!--See https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/spring.md -->
              <sourceFolder>src/main/java</sourceFolder>
              <bigDecimalAsString>true</bigDecimalAsString>
              <dateLibrary>java8</dateLibrary>
              <interfaceOnly>true</interfaceOnly>
              <library>spring-boot</library>
              <skipDefaultInterface>true</skipDefaultInterface>
            </configOptions>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

我的yaml文件是非常基本的,如果你需要看到它:

openapi: 3.0.0
info:
  description: StackOverflow ResponseEntity Question
  version: 1.0.0
  title: ResponseEntity Question
paths:
  /student:
    get:
      summary: Get all students
      operationId: getAllStudents
      responses:
        200:
          description: Get all students
          content: 
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Student'

components: 
  schemas: 
    Student:
      type: object
      properties:
        name:
          type: string
        id:
          type: integer
          format: int64
      required:
        - name
        - id
gdrx4gfi

gdrx4gfi1#

就像这里How to prevent Mono Request body generation when generating the API interfaces for spring-webflux with open api code generator?你可以覆盖/自定义swagger-codegen模板中的api.mustache
在这里你可以找到原始模板https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache
所以你可以把你自己的模板放在src/main/java/resource里面。

openApiGenerate {   
    generatorName = "spring"
    library = "spring-boot"
    ...
    configOptions = [
    ...
    templateDirectory: "src/main/resources/openapi-templates"
    ...
    ]
}

以下是关于模板https://github.com/OpenAPITools/openapi-generator/blob/master/docs/templating.md的文档

相关问题