【问题标题】:How do you change the date format in Swagger documentation?如何更改 Swagger 文档中的日期格式?
【发布时间】:2019-03-15 15:19:59
【问题描述】:

我的模型中有以下内容,如https://stackoverflow.com/a/34750537/148844中所述

@ApiModelProperty(required = true, dataType = "java.time.LocalDate")
@JsonFormat(pattern="yyyy-MM-dd")
private Date mCreatedAt;

但是 Swagger 仍将日期显示为带日期时间的区域。我也试过org.joda.time.LocalDate。如何更改文档日期格式示例?

这是关于该属性的文档。

http://docs.swagger.io/swagger-core/v1.3.12/apidocs/index.html?com/wordnik/swagger/annotations/ApiModelProperty.html

SpringFox-Swagger-UI 2.9.2


我在运行时在 Swagger UI 顶部注意到了这个错误。

错误
路径解析器错误。/getTrackingDataByUserID.post.responses.200.schema.properties.items.items.properties.mCreatedAt.$ref
无法解析引用,因为:无法解析指针:/definitions/LocalDate 在文档中不存在

【问题讨论】:

    标签: java swagger swagger-ui


    【解决方案1】:

    您需要使用java.sql.Date 而不是java.time.LocalDate。如果您对映射到的内容感兴趣,请查看springfox.documentation.schema.Types。这是完整的例子:

    @JsonFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(dataType = "java.sql.Date")
    private Date birthDate;
    

    ,这将生成以下内容:

    properties: {
      birthDate: {
         type: "string",
         format: "date"
      }
    }
    

    这里是springfox.documentation.schema.Types的相关内容:

    private static final Map<Type, String> typeNameLookup = ImmutableMap.<Type, String>builder()
      .put(Long.TYPE, "long")
      .put(Short.TYPE, "int")
      .put(Integer.TYPE, "int")
      .put(Double.TYPE, "double")
      .put(Float.TYPE, "float")
      .put(Byte.TYPE, "byte")
      .put(Boolean.TYPE, "boolean")
      .put(Character.TYPE, "string")
      .put(Date.class, "date-time")
      .put(java.sql.Date.class, "date")
      .put(String.class, "string")
      .put(Object.class, "object")
      .put(Long.class, "long")
      .put(Integer.class, "int")
      .put(Short.class, "int")
      .put(Double.class, "double")
      .put(Float.class, "float")
      .put(Boolean.class, "boolean")
      .put(Byte.class, "byte")
      .put(BigDecimal.class, "bigdecimal")
      .put(BigInteger.class, "biginteger")
      .put(Currency.class, "string")
      .put(UUID.class, "uuid")
      .put(MultipartFile.class, "__file")
      .build();
    

    【讨论】:

    • 我不认为java.sql.Date 类型会在 REST API 公开的 DTO 中泄漏。
    • Types 类现在已弃用,但 springfox.documentation.schema.ScalarTypes 中的 SCALAR_TYPE_LOOKUP 大致相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 2018-10-12
    • 2016-03-08
    • 2020-12-17
    • 2020-05-22
    相关资源
    最近更新 更多