java - 有什么方法可以在 Spring 中使用 Swagger 包含 Controller 中未使用的类吗?

标签 java spring yaml swagger swagger-maven-plugin

我正在使用 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"

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

我的插件设置:

    <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 而无需在任何 Controller 中使用它?

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

最佳答案

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

方法级别:

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

关于java - 有什么方法可以在 Spring 中使用 Swagger 包含 Controller 中未使用的类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63991515/

相关文章:

r - 如何在 Markdown 中调整 R 输出表的大小?

azure - 从 YAML 配置 azure-pipelines 中的构建中排除项目

Java:生成 JSON:尝试命名对象和数组时出现异常

java - 没有 JDBC 类型 : 1111 的方言映射

spring - 在测试服务器中出现错误 504,但在本地却没有出现错误

spring - 当请求正文未通过使用 Bean Validation/Hibernate Validator 定义的验证时,如何返回自定义响应 pojo?

json - 使用 Groovy 将 yaml 转为 json

java - 如何设置我的请求焦点事件监听器线程安全?

java - 如何纠正数组索引越界异常

Java字正则表达式