spring - 在 Springfox 中用 "type":"string"和 "format":

简介

在一个简单且裸露的 spring-boot 项目中,我有一个模型类,它具有 java.time.LocalTime 类型的单个属性。
如果我运行该项目,springfox 会将 LocalTime 检测为非内置类并将其置于 definitions 下。

{
  "ExampleDto": {
    "type": "object",
    "properties": {
      "time": {
        "$ref": "#/definitions/LocalTime"
      }
    }
  }
}

不是我想要的。


我真正想要的

... 是拥有一个字符串类型的属性,其自定义格式名为“时间”,如下所示:

{
  "ExampleDto": {
    "type": "object",
    "properties": {
      "time": {
        "type": "string",
        "format": "time"
      }
    }
  }
}

我能想出什么

如果我从 LocalTime 添加一个 AlternateTypeRuleString,我只能得到我想要的一半。

{
  "ExampleDto": {
    "type": "object",
    "properties": {
      "time": {
        "type": "string"
      }
    }
  }
}

当然,我也尝试了很多东西,但我从来没有设法设置属性的“格式”。 我发现以某种方式调用构造函数 io.swagger.models.properties.StringProperty#StringProperty(java.lang.String) 以“时间”作为参数可以解决问题,但我不知道如何实现这一目标。

我在 github 上建立了一个项目来展示我的问题。 https://github.com/LorenzSchumann/springfoxshowcase

最佳答案

刚刚在 Springfox documentation 中找到了关闭建议:

Q. How do we use Java 8 types easily. LocalDateTime?

The easiest way to to configure dates is via Docket#directModelSubstitute(LocalDateTime.class, String.class). If these are ISO 8601 dates that conform to a string format i.e. yyyy-MM-dd’T’HH:mm’Z'. However you won’t have any format or validation info.

The way to correctly map the "Date" and "DateTime" types to their corresponding swagger types:

  • Substitute "Date" types (java.util.LocalDate, org.joda.time.LocalDate) by java.sql.Date.
  • Substitute "DateTime" types (java.util.ZonedDateTime, org.joda.time.LocalDateTime, …​) by java.util.Date.

Example:

docket
  .directModelSubstitute(LocalDate.class, java.sql.Date.class)
  .directModelSubstitute(LocalDateTime.class, java.util.Date.class)

在您的情况下,它将是:

docket.directModelSubstitute(LocalTime.class, java.util.Date.class)

之后的输出将是:

...
"properties": {
  "time": {
    "type": "string",
    "format": "date-time"
  }
}
...

关于spring - 在 Springfox 中用 "type":"string"和 "format":"time"替换模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54368276/

相关文章:

jquery - Parsley.js isValid() 使用自定义验证器返回 null

laravel - 在 Scout 中使用更复杂的 where 子句

c# - 继续获取 "Unable to load DLL ' SQLite.Interop.dll

nginx - 使用 apollo 客户端从 nextjs 中的 getInitialProps 方

phpunit - CakePHP 2 代码覆盖率现在显示为 "No files to genera

angular6 - 我想根据复选框选择显示/隐藏 Angular 6 react 形式组

angular - 如何修复 Angular 错误 : TS1039 in compiling?

bash - ssh-add <(echo "$SSH_PRIVATE_KEY") 在 gitlab

javascript - PokeAPI : get complex evolution chain

python - virtualenv 没有匹配的分布