Swagger注释不会产生预期结果

时间:2018-04-17 06:33:20

标签: java spring annotations swagger

当我们使用ApiModelProperty和示例定义字符串属性时,我的团队正在使用swagger注释1.5.14生成文档的swagger文件:

@ApiModelProperty(example="484799")
private String accountNumber;

这会生成输出:

"accountNumber": 484799

是否可以使用示例值双引号生成帐号:

"accountNumber": "484799"

因为在查看示例时,更容易在字符串值和数值之间进行判断。

到目前为止,我们已经尝试过:

  1. 将转义字符设为双引号(例如=" \" 484799 \"")
  2. 使用dataType =" java.lang.String"使用示例参数
  3. 在示例值中留出额外空格。
  4. 我的环境:Java 1.8,swagger注释1.5.14,swagger 2

    提前致谢

3 个答案:

答案 0 :(得分:1)

我找到了这个问题的原因,它是在Springfox库中的Swagger2JacksonModule类,有一个基于值的方法检查:

 private boolean isNotJsonString(final String value) throws IOException {
    // strictly speaking, should also test for equals("null") since {"example": null} would be valid JSON
    // but swagger2 does not support null values
    // and an example value of "null" probably does not make much sense anyway
    return value.startsWith("{")                              // object
        || value.startsWith("[")                          // array
        || "true".equals(value)                           // true
        || "false".equals(value)                          // false
        || JSON_NUMBER_PATTERN.matcher(value).matches();  // number
  }

仅检查值,但忽略在注释上声明的dataType。

答案 1 :(得分:0)

您可以在@ApiModelProperty中使用'dataType'元素属性。

@ApiModelProperty(datatype= "String", example="484799")
private String accountNumber;

@ApiModelProperty(datatype= "java.lang.String", example="484799")
private String accountNumber;

如果使用的是Swagger2,则@Schema是选项。 https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#schema

答案 2 :(得分:-1)

也许你可以在下面以类似的方式使用String.format()

String example="484799"
private String accountNumber = String.format("%s", example)
相关问题