简体   繁体   English

Java springdoc-openapi 在 Swagger UI 示例值中显示带有附加日期/时间字段的 LocalDateTime 字段

[英]Java springdoc-openapi show LocalDateTime field with additional date/time fields in Swagger UI Example Value

spring-boot 2.6.2 spring-boot 2.6.2
springdoc-openapi 1.6.2 springdoc-openapi 1.6.2

Single field LocalDateTime dateTime in body represent in Swagger UI Example Value as正文中的单个字段LocalDateTime dateTime时间在 Swagger UI 示例中表示为

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

Date format can be fixed ( https://ru.stackoverflow.com/a/1276885/209226 ) by日期格式可以通过以下方式固定( 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);
    }

But it is still remained 3 fields iso one dateTime :但它仍然保留 3 个字段 iso 一个dateTime

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

How can it be fixed?如何修复?

if you want a better representation you need to add a type, in my case string.如果你想要一个更好的表示,你需要添加一个类型,在我的例子中是字符串。 See Docs ( https://swagger.io/specification/ )请参阅文档( 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);
        }

For Kotlin Developer:对于 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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM