大摇大摆地使用java。时间Instant表示日期时间,而不是OffsetDateTime
我正在使用openApi maven plugin为RESTAPI生成java请求/响应
请求有一个DateTime属性,当我运行生成器时,我会得到表示为java的属性的DateTime属性。时间OffsetDateTime。问题是我需要将属性表示为java。时间即刻的
这是请求的openApi规范:
"DocumentDto" : {
"type" : "object",
"properties" : {
"uuid" : {
"type" : "string",
"format" : "uuid"
},
"creationDate" : {
"type" : "string",
"format" : "date-time"
}
}
}
生成的java请求:
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2019-05-21T13:07:21.639+02:00[Europe/Zurich]")
public class DocumentDto {
public static final String SERIALIZED_NAME_UUID = "uuid";
@SerializedName(SERIALIZED_NAME_UUID)
private UUID uuid;
public static final String SERIALIZED_NAME_TEST = "creationDate";
@SerializedName(SERIALIZED_NAME_TEST)
private OffsetDateTime creationDate;
}
maven插件设置:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>3.3.4</version>
<executions>
<execution>
<id>test-service</id>
<phase>validate</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.build.directory}/open-api/swagger.json
</inputSpec>
<generatorName>java</generatorName>
<validateSpec>false</validateSpec>
<generateApis>false</generateApis>
<groupId>com.test</groupId>
<artifactId>test-service</artifactId>
<modelPackage>test.model</modelPackage>
<apiPackage>test.api</apiPackage>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
我已经按如下方式尝试了typeMappings
和importMappings
,但对生成的代码没有影响:
<typeMappings>DateTime=Instant</typeMappings>
<importMappings>Instant=java.time.Instant</importMappings>
# 1 楼答案
只需添加到openapi生成器maven插件的配置中
# 2 楼答案
正如@MaksimYakidovich所说,您确实需要添加映射,但也需要使用
"format" : "offset-date-time"
而不是date-time
# 3 楼答案
有相同的问题,但希望使用
LocalDateTime
而不是Instant
。添加以下工作,至少针对实体:也就是说,添加了
LocalDateTime
的导入,yaml中类型为format: date-time
的任何实体字段都映射到LocalDateTime
但是,对于api参数,没有使用这些设置添加导入。 过了一段时间,我放弃了,而是使用完全限定的类型,这样就不需要导入了。只需将上面的
typeMapping
更改为:之后生成的方法如下所示:
PS1请确保使用最新版本的插件
PS2我使用了委托模式
PS2我使用了弹簧模型
# 4 楼答案
您的更改将被生成器的
dateLibrary
配置覆盖。 要覆盖date
或date-time
格式类型,最好通过指定java生成器无法识别的值来禁用dateLibrary
将此添加到插件的
<configuration>
中以实现此目的:您可能还想添加
<jsr310>true</jsr310>
配置选项,因为当dateLibrary为java8
时,它是自动启用的(但就我所见,这主要是在生成客户机时使用的)# 5 楼答案
我知道问题是关于
openapi-generator-maven-plugin
,但由于为org.openapi.generator
gradle插件寻找相同的配置,我想这可能对其他人有用:与
org.openapi.generator
gradle插件等效的是: