Java swagger 日期字段与日期时间字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34726086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:55:53  来源:igfitidea点击:

swagger date field vs date-time field

javaswaggerswagger-uispringfoxswagger-maven-plugin

提问by Sourav

I am using swagger to test my rest api, one of the property of my entity class is a date field for which I need the date in yyyy-mm-dd format , but swagger model schema is showing this field as date-time instead of date field, therefore it gives date with time and zone. How can I convert this date-time into date field ?

我正在使用 swagger 来测试我的 rest api,我的实体类的属性之一是日期字段,我需要 yyyy-mm-dd 格式的日期,但是 swagger 模型模式将此字段显示为日期时间而不是date 字段,因此它给出了带有时间和区域的日期。如何将此日期时间转换为日期字段?

I have a java entity class TimeEntry.java one of its property is Date, it looks like this.

我有一个 java 实体类 TimeEntry.java,它的属性之一是 Date,它看起来像这样。

@ApiModelProperty(required = true)
@JsonFormat(pattern = DATE_FORMAT)
private Date date;

for this field, on the swagger UI model schema, the field date displays as "date": "2016-01-08T22:34:22.337Z" but I need this as "date":"2016-01-08" .

对于此字段,在 swagger UI 模型架构上,字段日期显示为 "date": "2016-01-08T22:34:22.337Z" 但我需要将其显示为 "date":"2016-01-08" 。

I have tried the following:

我尝试了以下方法:

1.

1.

@ApiModelProperty(required = true, dataType="date")  
@JsonFormat(pattern = DATE_FORMAT)   
private Date date;

2.Tried to follow along this code (override OverrideConvertor class) but could not find swagger-core 1.3 version mvn repo. Only available is 1.5 version https://github.com/swagger-api/swagger-core/wiki/overriding-models

2.尝试遵循此代码(覆盖 OverrideConvertor 类)但找不到 swagger-core 1.3 版本 mvn repo。仅可用 1.5 版本https://github.com/swagger-api/swagger-core/wiki/overriding-models

  1. Apparently from 1.5 version they have removed the OverrideConvertor class https://groups.google.com/forum/#!topic/swagger-swaggersocket/ChiknyHZiP4
  1. 显然从 1.5 版本他们已经删除了 OverrideConvertor 类 https://groups.google.com/forum/#!topic/swagger-swaggersocket/ChiknyHZiP4

Please help.

请帮忙。

回答by fehguy

The problem (one of the problems actually) with java.util.Dateis that it's really a date-time, and swagger correctly detects it as such. I do understand that the @JsonFormatis a workaround for this as well--swagger does not support that annotation during it's type detection.

问题(实际上java.util.Date是问题之一)是它确实是一个日期时间,并且 swagger 正确地检测到它。我确实理解这@JsonFormat也是一种解决方法——swagger 在类型检测期间不支持该注释。

You have three options to properly handle date types.

您有三个选项可以正确处理日期类型。

1) Use Joda's LocalDateas the datatype. If you declared private LocalDate date, it would appear correctly.

1) 使用 JodaLocalDate作为数据类型。如果您声明private LocalDate date,它将正确显示。

2) Use java8's LocalDate, same as above.

2) 使用 java8's LocalDate,同上。

3) Tell swagger to use either of the above when detecting the type in the annotation, but keep the property as type java.util.Date:

3) 在检测注解中的类型时告诉 swagger 使用上述任何一种,但保持属性为 type java.util.Date

@ApiModelProperty(required = true, dataType = "org.joda.time.LocalDate")

Then, when scanning, swagger will detect this as a dateformatted string.

然后,在扫描时,swagger 会将其检测为date格式化字符串。

回答by Sourav

My team mate has found the fix. We needed to upgrade the springfox version to 2.3.0 , previously we were using springfox 2.2.2 version. In that old version swagger's @ApiModelPreporty has attribute called "example" which was not doing anything. From the version 2.3.0 version this "example" started working. So after we upgraded the springfox version to 2.3.0 , all we had to do is as shown below.

我的队友找到了解决办法。我们需要将 springfox 版本升级到 2.3.0 ,之前我们使用的是 springfox 2.2.2 版本。在那个旧版本的 swagger 中,@ApiModelPreporty 有一个名为“example”的属性,它没有做任何事情。从 2.3.0 版本开始,这个“示例”开始工作。所以在我们将 springfox 版本升级到 2.3.0 之后,我们要做的就是如下图所示。

@ApiModelProperty(required = true,example = "2016-01-01")
@JsonFormat(pattern = DATE_FORMAT)
private LocalDate date; 

Below is the link from where we found this information:

以下是我们找到此信息的链接:

https://github.com/springfox/springfox/issues/998

https://github.com/springfox/springfox/issues/998