Java 8 LocalDate displaying in swagger

醉酒当歌 提交于 2019-12-08 12:02:35

问题


I have a DTO which contains field of Java 8 LocalDate type. With Jackson annotations it's possible to set format to ISO.DATE and everything works good. But Swagger (I have version 2.+) see the LocalDate.class as object

LocalDate {
month (integer, optional),
year (integer, optional)
}

(That's true but...) I want to dipsay this as string with format as it works with util.Date. How can I solve it?


回答1:


I was facing same problem, so I added

@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2)
                .groupName("name")
                .directModelSubstitute(LocalDateTime.class, String.class)
                .directModelSubstitute(LocalDate.class, String.class)
                .directModelSubstitute(LocalTime.class, String.class)
                .directModelSubstitute(ZonedDateTime.class, String.class)
                .apiInfo(apiInfo())
                .select()
                .paths(paths())
                .build();
}

in docket configuration.

directModelSubstitute makes swagger to treat LocalDate as String class



来源:https://stackoverflow.com/questions/40525468/java-8-localdate-displaying-in-swagger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!