我有一個描述我從 REST 服務獲得的資料的模式。我不能改變這個計劃。有兩個date-time在具有不同格式的架構型別欄位:
"date1": {
"type": "string",
"description": "Date 1",
"format": "date-time"
},
"date2": {
"type": "string",
"description": "Date 2",
"format": "date-time"
}
{
"date1": "2021-07-29T03:00:00",
"date2": "2021-04-22T08:25:30.264Z"
}
默認情況下,開放的 api-generator-maven-plugin 為date-time型別欄位創建 OffsetDateTime 型別:
@JsonProperty("date1")
private OffsetDateTime date1;
@JsonProperty("date2")
private OffsetDateTime date2;
隨著typeMappings和importMappings我可以代替OffsetDateTime到LocalDateTime:
<typeMappings>
<typeMapping>OffsetDateTime=LocalDateTime</typeMapping>
</typeMappings>
<importMappings>
<importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping>
</importMappings>
但是所有欄位都會發生這種替換:
@JsonProperty("date1")
private LocalDateTime date1;
@JsonProperty("date2")
private LocalDateTime date2;
有沒有辦法date1只用 LocalDateTime 替換 OffsetDateTime ?
這就是我想在生成的類中看到的:
@JsonProperty("date1")
private LocalDateTime date1;
@JsonProperty("date2")
private OffsetDateTime date2;
我知道我可以修復生成的類并將 OffsetDateTime 替換為 LocalDateTime,但是我不想在生成后每次都更改生成的類。
提前致謝。
uj5u.com熱心網友回復:
這是我最終得出的解決方案。我正在使用 maven-replacer-plugin 用 LocalDateTime 替換 OffsetDateTime:
<replacements>
<replacement>
<token>import java.time.OffsetDateTime;</token>
<value>import java.time.OffsetDateTime;\nimport java.time.LocalDateTime;</value>
</replacement>
<replacement>
<token>OffsetDateTime date1</token>
<value>LocalDateTime date1</value>
</replacement>
</replacements>
不是很優雅,但它有效)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357297.html
上一篇:我不斷收到錯誤無法在指定路徑/usr/lib/jvm/java-8-openjdk-amd64/jre/../lib/jconsole.jar處找到工件sun.jdk:jconsole:jar:jdk
