如何在Swagger中将Java8 LocalTime显示为字符串?

时间:2019-06-06 05:16:07

标签: java swagger swagger-ui swagger-2.0

我有 swagger 2.8.0 ,我的POJO类如下,

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
}

现在在swagger-ui中,在示例值部分中,我的POJO模型显示为

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

如何在swagger-ui中将LocalTime显示为字符串?

1 个答案:

答案 0 :(得分:1)

在大摇大摆的配置中尝试

directModelSubstitute 将解决此问题

@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());
   }