为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕慕森 2023-01-05 17:14:39
我有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 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

在大摇大摆的配置中试试这个


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

   }


查看完整回答
反对 回复 2023-01-05
  • 1 回答
  • 0 关注
  • 176 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信