7

I have swagger 2.8.0 and My POJO class is as follows,

public class Item {

   @JsonFormat(pattern="yyyy-MM-dd")
   private LocalDate date;

   @JsonFormat(pattern="HH:mm")
   private LocalTime time;

   // other fields and Getters and Setters are omitted for brevity
}

Now in the swagger-ui, in the example value section, my POJO model is shown as

{
  "date": "string",
  "time": {
    "hour": 0,
    "minute": 0,
    "nano": 0,
    "second": 0
  }
}

How to show the LocalTime as a string in the swagger-ui?

soorapadman
  • 4,451
  • 7
  • 35
  • 47
Dulanjaya Tennekoon
  • 2,408
  • 1
  • 18
  • 31
  • Related to and possible duplicate of [serialize/deserialize java 8 java.time with Jackson JSON mapper](https://stackoverflow.com/questions/27952472/serialize-deserialize-java-8-java-time-with-jackson-json-mapper) – Naman Jun 06 '19 at 05:32
  • 3
    The above one is related to marshalling and unmarshalling of java8 dates. But this question is regarding the swagger-ui, how to change the example value and model representation in swagger-ui. Thanks – Dulanjaya Tennekoon Jun 06 '19 at 05:36

2 Answers2

8

Try this in the swagger config

directModelSubstitute will resolve this issue

@Bean
   public Docket postsApi() {
      return new Docket(DocumentationType.SWAGGER_2)//.groupName("public-api")
              .groupName("")
                .directModelSubstitute(LocalDateTime.class, String.class)
               .directModelSubstitute(LocalDate.class, String.class)
               .directModelSubstitute(LocalTime.class, String.class)
               .directModelSubstitute(ZonedDateTime.class, String.class)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))

            .paths(PathSelectors.any())
            .paths(postPaths()).build().useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, getCustomizedResponseMessages());
   }
soorapadman
  • 4,451
  • 7
  • 35
  • 47
0

For springdoc-openapi-starter-webmvc-ui you can add Schema annotation

@Schema(type = "String", pattern = "HH:mm:SS")
LocalTime localTime