【问题标题】:Java springdoc-openapi show LocalDateTime field with additional date/time fields in Swagger UI Example ValueJava springdoc-openapi 在 Swagger UI 示例值中显示带有附加日期/时间字段的 LocalDateTime 字段
【发布时间】:2022-08-19 18:44:33
【问题描述】:

弹簧启动 2.6.2
springdoc-openapi 1.6.2

单身的正文中的字段LocalDateTime dateTime 在 Swagger UI 示例值中表示为

{
  \"dateTime\": \"2022-01-21T10:02:46.481Z\",
  \"time\": {
    \"hour\": 0,
    \"minute\": 0,
    \"second\": 0,
    \"nano\": 0
  },
  \"date\": \"2022-01-21\"
}

日期格式可以通过以下方式固定 (https://ru.stackoverflow.com/a/1276885/209226)

    static {
        var schema = new Schema<LocalTime>();
        schema.example(LocalTime.now().format(DateTimeFormatter.ISO_TIME));
        SpringDocUtils.getConfig().replaceWithSchema(LocalTime.class, schema);
    }

但它仍然是 3 个字段 iso 一个 dateTime:

{
  \"dateTime\": \"2022-01-21T10:05:52.945Z\",
  \"time\": \"13:05:14.746\",
  \"date\": \"2022-01-21\"
}

如何修复?

    标签: java spring-boot openapi springdoc localdatetime


    【解决方案1】:

    如果你想要一个更好的表示,你需要添加一个类型,在我的例子中是字符串。 查看文档 (https://swagger.io/specification/)

    static {
                var schema = new Schema<LocalTime>();
                schema.example(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))).type("string");
                SpringDocUtils.getConfig().replaceWithSchema(LocalTime.class, schema);
            }
    

    【讨论】:

      【解决方案2】:

      对于 Kotlin 开发人员:

      @Configuration
      open class SwaggerConfig{
          init {
              val schema: Schema<LocalDateTime> = Schema<LocalDateTime>()
              schema.example(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
              SpringDocUtils.getConfig().replaceWithSchema(LocalDateTime::class.java, schema)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-07
        • 1970-01-01
        • 2021-04-30
        • 2020-09-09
        • 2019-09-23
        • 2021-07-27
        • 1970-01-01
        相关资源
        最近更新 更多