有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

大摇大摆地使用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>

我已经按如下方式尝试了typeMappingsimportMappings,但对生成的代码没有影响:

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

共 (5) 个答案

  1. # 1 楼答案

    只需添加到openapi生成器maven插件的配置中

    <typeMappings>
         <typeMapping>OffsetDateTime=Instant</typeMapping>
    </typeMappings>
    <importMappings>                                
         <importMapping>java.time.OffsetDateTime=java.time.Instant</importMapping>
    </importMappings>
    
  2. # 2 楼答案

    正如@MaksimYakidovich所说,您确实需要添加映射,但也需要使用 "format" : "offset-date-time"而不是date-time

  3. # 3 楼答案

    有相同的问题,但希望使用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我使用了弹簧模型

  4. # 4 楼答案

    您的更改将被生成器的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时,它是自动启用的(但就我所见,这主要是在生成客户机时使用的)

  5. # 5 楼答案

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

    org.openapi.generatorgradle插件等效的是:

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