如何在Swagger文档中更改日期格式?

11

根据https://dev59.com/KVsW5IYBdhLWcg3wqYt7#34750537中的描述,我的模型中有以下内容:

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

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

swagger api docs

这是该属性的文档。

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顶部出现了以下错误:

错误
在paths./getTrackingDataByUserID.post.responses.200.schema.properties.items.items.properties.mCreatedAt.$ref处的Resolver出错
由于以下原因无法解析引用: Could not resolve pointer: /definitions/LocalDate does not exist in document

1个回答

2
你需要使用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();

7
我认为 java.sql.Date 类型不应该在 REST API 中暴露的 DTO 中泄漏。 - Marc Bouvier
Types类现在已经被弃用,但是springfox.documentation.schema.ScalarTypes中的SCALAR_TYPE_LOOKUP基本相同。 - whistling_marmot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接