gpt4 book ai didi

spring - 在 Springfox 中用 "type":"string"和 "format":"time"替换模型属性

转载 作者:行者123 更新时间:2023-12-05 07:26:21 29 4
gpt4 key购买 nike

简介

在一个简单且裸露的 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com