首页 > 解决方案 > 如何将 swagger 文档设置为使用 dd/MM/yyyy 格式?

问题描述

我正在使用 spring-boot 和 Swagger 将日期解析为休息服务以记录 api。

在端点上,我只想有没有时间的日期。并且很容易实现@DateTimeFormat(pattern = "dd/MM/yyyy")。请注意,比起几个月,我更喜欢日子在先。

使用 curl 时端点工作正常,那么我认为这不是问题所在。例如设置当年 11 月 30 日,我可以用 Curl 调用它,如下所示:

curl -X GET "http://localhost:8085/app/api/endpoint?creationTime=30%2F11%2F2020" -H  "accept: application/json"

并且工作正常。但是,如果我直接使用 Swagger 页面进行测试,我会遇到以下情况:

首先,如果我将日期设置为 30/11/2020,swagger 不允许我继续:

在此处输入图像描述

如果我将日期设置为 2020 年 11 月 30 日,swagger 允许我继续,但我收到此错误:

"message": "Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiParam @org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '11/30/2020'; nested exception is org.joda.time.IllegalFieldValueException: Cannot parse \"11/30/2020\": Value 30 for monthOfYear must be in the range [1,12]",

正如我所说,那是第 30 个月。对我来说很有意义。

如果我设置为 2020 年 5 月 11 日,Swagger 允许我发送请求,最后日期是 11 月 5 日,这是我定义的格式。

对我来说,swagger 似乎是按MM/dd/yyyy格式过滤我,而不是使用dd/MM/yyyy在端点上真正定义的我。是否有任何选项可以将 swagger 验证设置为dd/MM/yyyy

注意:使用 swagger 版本2.9.2。春季启动版本2.1.8.RELEASE

标签: javaspring-bootdateswagger

解决方案


推荐阅读