在swagger中使用现有项目中其他模型中的swagger

时间:2019-06-21 15:58:11

标签: swagger swagger-2.0 swagger-codegen

这是上下文: 我正在使用swagger通过swagger代码生成器构建API的结构 但是我的第一个问题是,实际上该API将具有Common-Entities的依赖关系。

My-Java-Swagger-Project
   -Controller
   -Entities(model)
   -Everything else
My-commons-project
   -Common Entities

因此,如果Swagger生成器将构建“ My-java-swagger-project”,而我将在其上使用My-commons-project的实体,那么如何指示swagger生成并使用Commons项目的现有类,或者不构建我在Commons项目中已经拥有的实体

definitions:
  Response:
    type: "object"
    properties:
      status:
        type: "string"
        description: "Returns if it was successful or not. "
      errors:
        type: "array"
        items: 
          $ref: "#/definitions/ErrorTO"
  Request:
    type: "object"
    required:
      - subject
      - messageText
    properties:
      subject:
        type: "string"
      messageText:
        type: "string"
  ErrorTO: #This one must not be defined in swagger because it's on my #commons project
    type: object
    properties:
      code:
        type: "string"
      description:
        type: "string"

当我运行 Swagger代码源输出ErrorTO实体,但是我已经在其他项目中时,那么如何定义swagger以不创建该实体并使用我的一个共同点(或仅用于不要创建它)

1 个答案:

答案 0 :(得分:0)

您可以在代码生成中定义importMappingstypeMappings来告诉它使用导入的类。不知道您使用的是哪个生成器,但是我对Java / spring-boot服务器存根生成器进行了起诉。我起诉了Maven插件,类似

 <plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>${swagger.codegen.version}</version>
    <executions>
      <execution>
        <id>foo</id>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
          <importMappings>
            <importMapping>Foo=com.example.common.Foo</importMapping>
          </importMappings>
          <typeMappings>
            <typeMapping>Foo=com.example.common.Foo</typeMapping>
          <typeMappings>
        </configuration>
      </execution>
 </plugin>
相关问题