spring-boot 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 1 dateTime
:
{
"dateTime": "2022-01-21T10:05:52.945Z",
"time": "13:05:14.746",
"date": "2022-01-21"
}
更新:
在 LocalDateTime dateTime
字段的对象中,我有辅助方法 getDate/getTime
。
public LocalDate getDate() {
return dateTime.toLocalDate();
}
public LocalTime getTime() {
return dateTime.toLocalTime();
}
它们在 json 中被忽略,就像基于字段的 jackson 配置一样,但在 springdoc 中不会忽略。
使用 @Schema(hidden = true)
最佳答案
如果你想要更好的表示,你需要添加一个类型,在我的例子中是字符串。 请参阅文档 ( 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);
}
关于Java springdoc-openapi 在 Swagger UI 示例值中显示 LocalDateTime 字段以及附加日期/时间字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70799677/