简体   繁体   English

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

[英]Is there any way to include classes that are not used in controller with Swagger in Spring?

I'm using swagger-maven-plugin(kongchen) to generate static document and I would like to generate yaml like this:我正在使用 swagger-maven-plugin(kongchen) 生成 static 文档,我想像这样生成 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"

But i would also want plugin to include classes that are not defined in my controller.但我也希望插件包含我的 controller 中未定义的类。

my plugin setup:我的插件设置:

    <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>

Is there any way to achieve this goal using Swagger?有没有办法使用 Swagger 来实现这个目标? The main goal of it is to have yaml that i can import to Apicurio and use in my applications.它的主要目标是拥有 yaml,我可以将其导入 Apicurio 并在我的应用程序中使用。

Or maybe is there any way to generate yaml that includes all classes like this without using it in any controller?或者也许有什么方法可以生成 yaml,其中包括所有这样的类,而无需在任何 controller 中使用它?

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

Do you mean to include the models, request / response stubs?您的意思是包括模型、请求/响应存根吗? If so you can use Swagger annotations如果是这样,您可以使用 Swagger 注释

Method Level:方法级别:

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

It is possible with micronaut.openapi to include yml files. micronaut.openapi 可以包含 yml 文件。 For example, if you have an openapi schema in openapi/mySchema.yml , you can import it to your schema by putting the following line in openapi.properties :例如,如果您在openapi/mySchema.yml mySchema.yml 中有一个 openapi 架构,您可以通过将以下行放入openapi.properties将其导入到您的架构中:

micronaut.openapi.additional.files=openapi

Unfortunately this approach doesn't work if you need to use the class. You can have a class defined separately but you need to keep it in sync with mySchema.yml manually.不幸的是,如果您需要使用 class,则此方法不起作用。您可以单独定义 class,但您需要手动将其与 mySchema.yml 保持同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swagger (Springfox) 只找到控制器 @RequestBody (Spring Boot) 中使用的模型 - Swagger (Springfox) only finding Models used in Controller @RequestBody (Spring Boot) 有没有办法指定要包含在 Spring Data MongoDB 中的特定存储库类? - Is there a way to specify specific repository classes to include in Spring Data MongoDB? 如何在项目目录中包含 swagger 生成的类 - How to include swagger generated classes in project directory Swagger - Controller 不包含在 OpenApi 文档中 - Swagger - Controller not include in OpenApi-Documentation 有没有办法通过对不同端点资源使用的模型类的 swagger 注释来编写不同的文档? - Is there any way to write different documentation through swagger annotation for model class which used by different end point resources? 从Swagger / OpenAPI生成Spring MVC控制器 - Generate Spring MVC controller from Swagger/OpenAPI 使用jarjarlinks在jar中仅包含使用过的类 - Using jarjarlinks to include only used classes in a jar Spring MVC中的@Controller注释和控制器类 - @Controller annotation and Controller Classes in Spring MVC 有什么方法可以在Eclipse中隐藏/折叠招摇注解? - Any way to hide/fold swagger annotations in Eclipse? 如何将src / test / java bean包含到任何 <name> 使用spring.test.java的启动方式? - How to include src/test/java beans to any <name>Test.java using spring boot way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM