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

Chl*_*loe 5 java swagger swagger-ui

/sf/answers/2432537621/中所述,我的模型中有以下内容

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

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

swagger api文档

这是该属性的文档。

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在文档中不存在

roz*_*zky 2

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

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

,这将生成以下内容:

properties: {
  birthDate: {
     type: "string",
     format: "date"
  }
}
Run Code Online (Sandbox Code Playgroud)

以下是相关内容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();
Run Code Online (Sandbox Code Playgroud)

  • 我不认为“java.sql.Date”类型会在 REST API 公开的 DTO 中泄漏。 (7认同)