|
2,268 total views, 12 views today
Swagger UI 的頁面中,請求的數據類型會被序列化成字符串,顯示在 Model Schema 中。
但是,Java8 中的 LocalDateTime 類型會被序列化成很復雜的字符串,如下圖。

解決的辦法其實很簡單,在 Swagger 的配置中,添加 directModelSubstitute 方法的代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
@Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .directModelSubstitute(LocalDateTime.class, Date.class) .directModelSubstitute(LocalDate.class, String.class) .directModelSubstitute(LocalTime.class, String.class) .directModelSubstitute(ZonedDateTime.class, String.class) .apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.abcd.restful")).paths(PathSelectors.any()).build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Platform API").contact("abcd").version("1.0").build(); } } |
directModelSubstitute 方法顧名思義就是在序列化的時候用一個類型代替一個類型。
上面的例子,LocalDateTime 類型用 Date 類型替代,LocalDate 類型直接用 String 類型替代,這樣就避免的 Swagger 原生的序列化方法把 LocalDateTime 序列化的很復雜。效果如下:

原創(chuàng)文章,轉載請注明出處!http://www./swagger序列化localdatetime的優(yōu)化/
|