swagger date field vs date-time field

前端 未结 2 859
栀梦
栀梦 2020-12-15 09:04

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 s

相关标签:
2条回答
  • 2020-12-15 09:17

    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.

    @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

    0 讨论(0)
  • 2020-12-15 09:24

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

    You have three options to properly handle date types.

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

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

    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:

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

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

    0 讨论(0)
提交回复
热议问题