Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Java8 LocalTime as a string in Swagger?

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?

like image 348
Dulanjaya Tennekoon Avatar asked Jun 06 '19 05:06

Dulanjaya Tennekoon


1 Answers

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());
   }
like image 125
soorapadman Avatar answered Nov 18 '22 23:11

soorapadman