【问题标题】:Is there any way to include classes that are not used in controller with Swagger in Spring?有没有办法在 Spring 中使用 Swagger 包含控制器中未使用的类?
【发布时间】:2021-01-07 12:05:34
【问题描述】:

我正在使用 swagger-maven-plugin(kongchen) 生成静态文档和 我想像这样生成 yaml:

swagger: "2.0"
info:
  version: "1.0.0"
  title: "Swagger example"
paths:
  /api/students:
    post:
      operationId: "addStudent"
      parameters:
      - in: "body"
        name: "body"
        required: false
        schema:
          $ref: "#/definitions/Student"
      responses:
        200:
          description: "successful operation"
          schema:
            type: "boolean"
definitions:
  Student:
    type: "object"
    properties:
      id:
        type: "integer"
        format: "int32"
        minimum: 1
        maximum: 20
      name:
        type: "string"
      surname:
        type: "string"

但我也希望插件包含未在我的控制器中定义的类。

我的插件设置:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>3.1.8</version>
                <configuration>
                    <apiSources>
                        <apiSource>
                            <springmvc>true</springmvc>
                            <locations>
                                <location>
                                    mypackage
                                </location>
                            </locations>
                            <info>
                                <title>
                                    Swagger example
                                </title>
                                <version>
                                    1.0.0
                                </version>
                            </info>
                            <outputFormats>json,yaml</outputFormats>
                            <swaggerDirectory>generated</swaggerDirectory>
                            <swaggerApiReader>com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader</swaggerApiReader>
                        </apiSource>
                    </apiSources>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <!-- Adding dependency to swagger-hibernate-validations to enable the BeanValidator as a custom
                         model converter -->
                    <dependency>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-hibernate-validations</artifactId>
                        <version>1.5.6</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

有没有什么方法可以使用 Swagger 实现这个目标? 它的主要目标是拥有可以导入到 Apicurio 并在我的应用程序中使用的 yaml。

或者也许有什么方法可以生成包含所有此类的 yaml,而无需在任何控制器中使用它?

 @ApiModel
    public class Student {
        @Min(1)
        @Max(20)
        @ApiModelProperty
        private int id;
        @ApiModelProperty
        private String name;}

【问题讨论】:

  • 我的控制器中没有定义的类是什么意思?
  • 例如我有 Teacher 类,但它没有在我的控制器中使用,我想将它们包含在 yaml 文件中。
  • 但是记录普通用户无法交互的东西有什么用呢?
  • 所以我想拥有一些可以在我所有的前端和后端应用程序中使用的共享类。我正在使用 apicurio,但我想通过在我的实体上添加注释来验证请求。

标签: java spring yaml swagger swagger-maven-plugin


【解决方案1】:

micronaut.openapi 可以包含 yml 文件。例如,如果您在 openapi/mySchema.yml 中有一个 openapi 架构,您可以通过在 openapi.properties 中输入以下行来将其导入您的架构:

micronaut.openapi.additional.files=openapi

不幸的是,如果您需要使用该类,这种方法不起作用。您可以单独定义一个类,但您需要手动使其与 mySchema.yml 保持同步。

【讨论】:

    【解决方案2】:

    您的意思是包含模型、请求/响应存根吗? 如果是这样,您可以使用 Swagger 注释

    方法级别:

    @ApiOperation(value = "Get Details", response = ResponseClassStub, produces = MediaType.APPLICATION_JSON_VALUE)
    

    【讨论】:

    • 很遗憾没有。我想为类生成模式,但不需要在控制器中声明它们。就像在 Apicurio 中一样,您可以创建不在任何端点中使用的数据类型
    猜你喜欢
    • 2020-08-15
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 2022-01-25
    相关资源
    最近更新 更多