簡體   English   中英

恩惠在招搖

[英]Enum in swagger

我想知道如何以昂首闊步的方式記錄枚舉。

根據JavaDoc

dataType。 有關支持的數據類型,請參閱文檔。 如果數據類型是自定義對象,請設置其名稱,或者不設置任何名稱。 在枚舉的情況下,使用'string'和allowableValues作為枚舉常量。

但我沒有找到一些好的Java示例如何真正使用它,規范在這里

Java的

第一服務

package betlista.tests.swagger;

import betlista.tests.swagger.model.Input;
import betlista.tests.swagger.model.Output;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "first", position = 1)
public class RestServiceFirst {

    @ApiOperation(value = "foo1 operation", httpMethod = "POST", position = 1, nickname = "foo")
    public void foo1(Input input) {

    }

    @ApiOperation(value = "bar1 operation", response = Output.class, httpMethod = "GET", position = 2, nickname = "bar")
    public Output bar1() {
        return null;
    }

}

第二次服務

package betlista.tests.swagger;

import betlista.tests.swagger.model.Input;
import betlista.tests.swagger.model.Output;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "second", position = 2)
public class RestServiceSecond {

    @ApiOperation(value = "foo2 operation", httpMethod = "POST", position = 1)
    public void foo2(Input input) {

    }

    @ApiOperation(value = "bar2 operation", response = Output.class, httpMethod = "GET", position = 2)
    public Output bar2() {
        return null;
    }

}

輸入

package betlista.tests.swagger.model;

import com.wordnik.swagger.annotations.ApiModel;
import com.wordnik.swagger.annotations.ApiModelProperty;

@ApiModel
public class Input {

    @ApiModelProperty(dataType = "string", allowableValues = "M, T", value = "description", notes = "notes")
    public Day day;

}

package betlista.tests.swagger.model;

public enum Day {

    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;

}

產量

package betlista.tests.swagger.model;

import com.wordnik.swagger.annotations.ApiModel;

@ApiModel(value = "Output")
public class Output {

    @ApiModelProperty
    String field;

}

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>betlista</groupId>
    <artifactId>tests-swagger</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <!-- generate REST documentation -->
        <dependency>
            <groupId>com.wordnik</groupId>
            <artifactId>swagger-jaxrs_2.10</artifactId>
            <version>1.3.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <apiSources>
                        <apiSource>
                            <locations>betlista.tests.swagger;betlista.tests.swagger.model</locations>
                            <apiVersion>1.0.0</apiVersion>
                            <basePath>http://localhost:port/rest</basePath>
                            <outputTemplate>${basedir}/strapdown.html.mustache</outputTemplate>
                            <outputPath>${basedir}/target/generated/strapdown.html</outputPath>
                            <swaggerDirectory>${basedir}/target/generated/apidocs</swaggerDirectory>
                            <useOutputFlatStructure>false</useOutputFlatStructure>
                        </apiSource>
                    </apiSources>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

你可以在這里看到結果。

我看到HTML輸出中存在很多問題(缺少輸出描述,錯誤的URL,描述用於注釋),但我不知道如何克服的是枚舉用法。

tests-swagger\\target\\generated\\apidocs\\first.json應該是(我認為)

  "models" : {
    "Input" : {
      "id" : "Input",
      "description" : "",
      "properties" : {
        "day" : {
          "type" : "string",
          "enum" : [ "M", " T" ]
        }
      }
    }
  }

但是還有

  "models" : {
    "Input" : {
      "id" : "Input",
      "description" : "",
      "properties" : {
        "day" : {
          "$ref" : "Day",
          "enum" : [ "M", " T" ]
        }
      }
    }
  }

而且我認為$ref是個問題......

任何的想法?

在swagger-maven-plugin 3.1.0的情況下,這可能是一個最小的文檔:

@ApiModel
public class Input {
    @ApiModelProperty
    public Day day;
}

@ApiModel
public enum Day {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;
}

然后這是生成的json模型:

"definitions" : {
  "Input" : {
    "type" : "object",
    "properties" : {
      "day" : {
        "type" : "string",
        "enum" : [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ]
      }
    }
  }
}

這就是模型在SwaggerUI中的呈現方式:

Input {
day (string, optional) = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
}

根據你指出的文件:

dataType。 有關支持的數據類型,請參閱文檔。 如果數據類型是自定義對象,請設置其名稱,或者不設置任何名稱。 在枚舉的情況下,使用'string'和allowableValues作為枚舉常量。

我認為你應該手動添加枚舉值:

@ApiModel
public class Input {

    @ApiModelProperty(dataType = "string", allowableValues = "Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday", value = "description", notes = "notes")
    public Day day;
}

自定義Springfox插件解決方案:

swagger.io建議: “如果需要指定枚舉項的描述,可以在參數或屬性的描述中執行此操作”

我基於專有的@ApiEnum注釋實現了這個建議。 該庫可在此處獲取: https//github.com/hoereth/springfox-enum-plugin

在OpenApi版本Swagger 2.x中,您需要使用此處描述的新注釋: https//github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations

@Schema(description = "Shuttle shipment action")
public class ShuttleShipmentAction {

   @Schema(required = true, description = "Id of a shuttle shipments")
   private long id;

   @Schema(required = true, description = "Action to be performed on shuttle shipments", allowableValues = { "SEND",
        "AUTHORIZE", "REJECT", "FIX_TRAINRUN", "UNFIX_TRAINRUN", "DELETE" })
   private String action;
   ...
   ...

結果是這樣的: 在此輸入圖像描述

您可以將responseContainer與@ApiOperation注釋一起使用:

@ApiOperation(value = "Brief description of your operation.", response = YourEnum.class, responseContainer = "List")

謝謝你的幫助。

我在我的代碼中習慣了這種類型。

private String date;
@ApiModelProperty(dataType = "string", allowableValues = "FirstValue,   SecondValue", value = "description", notes = "notes")
private CarrierType carrierName;


public enum CarrierType {
    FirstValue,
    SecondValue
}

它對我來說很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM