我正在使用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>发布于 2019-09-18 20:39:37
只需添加到openapi-generator-maven-plugin的配置中
<typeMappings>
<typeMapping>OffsetDateTime=Instant</typeMapping>
</typeMappings>
<importMappings>
<importMapping>java.time.OffsetDateTime=java.time.Instant</importMapping>
</importMappings>发布于 2020-02-20 20:43:22
也有同样的问题,但希望使用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,我用的是弹簧模型。
发布于 2020-03-12 01:03:07
您所做的更改将被生成器的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时,它是自动启用的(但据我所知,这主要用于生成客户端时)。
https://stackoverflow.com/questions/56237650
复制相似问题