How do you change the date format in Swagger documentation?

15,123

You need to use java.sql.Date instead of java.time.LocalDate. If you interested in what is mapped to what check springfox.documentation.schema.Types. Here is full example:

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

,which will generate the following:

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

Here is relevant content of 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();
Share:
15,123
Chloe
Author by

Chloe

Updated on July 20, 2022

Comments

  • Chloe
    Chloe almost 2 years

    I have the following in my model as described in https://stackoverflow.com/a/34750537/148844

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

    However Swagger is still displaying the date as a date-time-with-zone. I also tried org.joda.time.LocalDate. How do I change the documentation date format example?

    swagger api docs

    Here is the documentation on the property.

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

    SpringFox-Swagger-UI 2.9.2


    I noticed this error at top of Swagger UI when run.

    Errors
    Resolver error at paths./getTrackingDataByUserID.post.responses.200.schema.properties.items.items.properties.mCreatedAt.$ref
    Could not resolve reference because of: Could not resolve pointer: /definitions/LocalDate does not exist in document

  • Marc Bouvier
    Marc Bouvier almost 4 years
    I don't think java.sql.Date type is intended to leak in DTOs exposed by a REST API.
  • whistling_marmot
    whistling_marmot almost 3 years
    The Types class is deprecated now, but SCALAR_TYPE_LOOKUP in springfox.documentation.schema.ScalarTypes is much the same.