我正在使用openApi maven plugin为REST api生成Java请求/响应。
该请求具有DateTime属性,当我运行生成器时,我获得了表示为java.time.OffsetDateTime的属性的DateTime属性。问题是我需要将属性表示为java.time.Instant。
这是请求的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>
答案 0 :(得分:4)
遇到相同的问题,但希望使用LocalDateTime
而不是Instant
。添加以下作品,至少适用于实体:
<configuration>
<typeMappings>
<typeMapping>OffsetDateTime=LocalDateTime</typeMapping>
</typeMappings>
<importMappings>
<importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping>
</importMappings>
</configuration>
即添加了LocalDateTime
的导入,并且Yaml中类型为format: date-time
的任何实体字段都映射到LocalDateTime
。
但是,对于api 参数,没有使用这些设置添加任何导入。
一段时间后,我放弃了,而是使用了完全限定的类型,因此不需要导入。只需将上面的typeMapping
更改为:
<typeMapping>OffsetDateTime=java.time.LocalDateTime</typeMapping>
您随后生成的方法如下:
default ResponseEntity<List<SomeDto>> someMethodGet(
@ApiParam(value = "") @Valid @RequestParam(value = "changeDateFrom",
required = false) @org.springframework.format.annotation.DateTimeFormat(
iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME) java.time.LocalDateTime changeDateFrom,
{
return getDelegate().someMethodGet(..., changeDateFrom, ...);
}
PS1确保使用最新版本的插件。
PS2我使用了委托模式。
PS2我使用了弹簧模型。
答案 1 :(得分:3)
您的更改被生成器的dateLibrary
配置所覆盖。
要覆盖date
或date-time
格式类型,最好通过指定Java生成器无法识别的值来禁用dateLibrary
。
将此添加到插件的<configuration>
中以实现此目的:
<configOptions>
<java8>true</java8>
<dateLibrary>custom</dateLibrary>
</configOptions>
<typeMappings>
<typeMapping>DateTime=Instant</typeMapping>
<typeMapping>Date=LocalDate</typeMapping>
</typeMappings>
<importMappings>
<importMapping>Instant=java.time.Instant</importMapping>
<importMapping>LocalDate=java.time.LocalDate</importMapping>
</importMappings>
您可能还需要添加<jsr310>true</jsr310>
配置选项,因为当dateLibrary为java8
时,该选项会自动启用(但据我所知,此选项通常在生成客户端时使用)。
答案 2 :(得分:2)
只需添加到openapi-generator-maven-plugin的配置中
<typeMappings>
<typeMapping>OffsetDateTime=Instant</typeMapping>
</typeMappings>
<importMappings>
<importMapping>java.time.OffsetDateTime=java.time.Instant</importMapping>
</importMappings>
答案 3 :(得分:0)
您确实必须像@MaksimYakidovich所说的那样添加映射,而且还需要使用
"format" : "offset-date-time"
代替date-time