小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Swagger序列化LocalDateTime的優(yōu)化

 wwq圖書世界 2019-11-19

Swagger 序列化 LocalDateTime 的優(yōu)化

wusq Java基礎 六月 20, 2019

2,268 total views, 12 views today

Swagger UI 的頁面中,請求的數據類型會被序列化成字符串,顯示在 Model Schema 中。

但是,Java8 中的 LocalDateTime 類型會被序列化成很復雜的字符串,如下圖。

解決的辦法其實很簡單,在 Swagger 的配置中,添加 directModelSubstitute 方法的代碼

Java
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)化/

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多