繁体   English   中英

使用 java.time.Instant 来表示 DateTime 而不是 OffsetDateTime

[英]Use java.time.Instant to represent DateTime instead of OffsetDateTime

我正在使用openApi maven 插件为 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>

我已经尝试了typeMappingsimportMappings如下但对生成的代码没有任何影响:

<typeMappings>DateTime=Instant</typeMappings>
<importMappings>Instant=java.time.Instant</importMappings>

只需添加到 openapi-generator-maven-plugin 的配置中

<typeMappings>
     <typeMapping>OffsetDateTime=Instant</typeMapping>
</typeMappings>
<importMappings>                                
     <importMapping>java.time.OffsetDateTime=java.time.Instant</importMapping>
</importMappings>

有同样的问题,但想使用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 parameters ,没有使用这些设置添加导入。 过了一会儿,我放弃了,而是使用完全限定的类型,这样就不需要导入了。 只需将上面的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我用的是弹簧模型。

您的更改会被生成器的dateLibrary配置覆盖。 要覆盖datedate-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时它会自动启用(但据我java8 ,这主要用于生成客户端)。

您确实必须像@MaksimYakidovich 所说的那样添加映射,但也要使用"format" : "offset-date-time"更改您的规范"format" : "offset-date-time"而不是date-time

我知道这个问题是关于openapi-generator-maven-plugin ,但是由于为org.openapi.generator gradle 插件寻找相同的配置把我带到这里,我想这可能对其他人有用:

org.openapi.generator gradle 插件的等价物是:

openApiGenerate {
    // ...
    typeMappings = [
        OffsetDateTime: "Instant"
    ]
    importMappings = [
        "java.time.OffsetDateTime": "java.time.Instant"
    ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM