首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用java.time.Instant代替OffsetDateTime来表示DateTime

使用java.time.Instant代替OffsetDateTime来表示DateTime
EN

Stack Overflow用户
提问于 2019-05-21 19:48:01
回答 5查看 15.9K关注 0票数 19

我正在使用openApi maven plugin为REST api生成java请求/响应。

该请求有一个DateTime属性,当我运行生成器时,我得到了表示为java.time.OffsetDateTime的属性的DateTime属性。问题是我需要将属性表示为java.time.Instant。

以下是该请求的openApi规范:

代码语言:javascript
复制
"DocumentDto" : {
      "type" : "object",
      "properties" : {
        "uuid" : {
          "type" : "string",
          "format" : "uuid"
        },
        "creationDate" : {
          "type" : "string",
          "format" : "date-time"
        }
      }
    }

生成的java请求:

代码语言:javascript
复制
@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插件设置:

代码语言:javascript
复制
<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,如下所示,但它对生成的代码没有影响:

代码语言:javascript
复制
<typeMappings>DateTime=Instant</typeMappings>
<importMappings>Instant=java.time.Instant</importMappings>
EN

回答 5

Stack Overflow用户

发布于 2019-09-18 20:39:37

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

代码语言:javascript
复制
<typeMappings>
     <typeMapping>OffsetDateTime=Instant</typeMapping>
</typeMappings>
<importMappings>                                
     <importMapping>java.time.OffsetDateTime=java.time.Instant</importMapping>
</importMappings>
票数 20
EN

Stack Overflow用户

发布于 2020-02-20 20:43:22

也有同样的问题,但希望使用LocalDateTime而不是Instant。添加以下工作,至少对于实体是这样的

代码语言:javascript
复制
<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更改为:

代码语言:javascript
复制
<typeMapping>OffsetDateTime=java.time.LocalDateTime</typeMapping>

您随后生成的方法如下所示:

代码语言:javascript
复制
    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,我用的是弹簧模型。

票数 10
EN

Stack Overflow用户

发布于 2020-03-12 01:03:07

您所做的更改将被生成器的dateLibrary配置覆盖。要覆盖datedate-time格式类型,最好通过指定java生成器无法识别的值来禁用dateLibrary

将此代码添加到插件的<configuration>中,以实现以下目标:

代码语言:javascript
复制
  <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时,它是自动启用的(但据我所知,这主要用于生成客户端时)。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56237650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档