【问题标题】:Need org.threeten.bp.LocalDate to return date in format "YYYY-MM-DD"需要 org.threeten.bp.LocalDate 以“YYYY-MM-DD”格式返回日期
【发布时间】:2019-01-03 19:19:23
【问题描述】:

我正在使用yaml 文件生成将用于返回JSON 响应的类。

yaml

NextPaymentDueDate:
    description: Date the next payment is due on the loan
    type: string
    example: '2018-07-04'
    format: date

我正在使用swagger-codegen-maven-plugin 来生成这些类:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <id>api-call</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>src/main/resources/search.yaml</inputSpec>
                <language>java</language>
                <dateLibrary>java8-localdatetime</dateLibrary>
                <output>${project.basedir}</output>
                <apiPackage>com.api</apiPackage>
                <modelPackage>com.model</modelPackage>
                <invokerPackage>com.client</invokerPackage>
            </configuration>
        </execution>
    </executions>
</plugin>

在我的代码中:

  @SerializedName("NextPaymentDueDate")
  private LocalDate nextPaymentDueDate = null;
...
  myObj.setNextPaymentDueDate(LocalDate.parse("2018-07-01"));

结果:

"nextPaymentDueDate": { <-- note lowercase
            "year": 2018,
            "month": "JULY",
            "era": "CE",
            "dayOfMonth": 1,
            "dayOfWeek": "SUNDAY",
            "dayOfYear": 182,
            "leapYear": false,
            "monthValue": 7,
            "chronology": {
                "id": "ISO",
                "calendarType": "iso8601"
            }
        },

我需要它返回:

"nextPaymentDueDate": {
            "2018-07-01"
        },

我也加了application.properties:

spring.jackson.serialization.write-dates-as-timestamps=false

但这并没有做任何事情。

【问题讨论】:

  • 如果您使用的是 Java 8,为什么还要使用 ThreeTen? ThreeTen 是 Java 6 和 7 使用的后向端口。java8-localdatetime 支持 Java 8 Time API,而不是 ThreeTen。要支持 ThreeTen,您需要编写和注册自己的 TypeAdapter 对象。
  • @Andreas 我希望我能控制使用什么:) 我似乎无法用 swager-codegen-plugin 覆盖 ThreeTen :(
  • 日期/时间类没有自己的格式,它们只是从指定锚点开始的时间量的容器。相反,您需要使用 DateTimeFormatter
  • 或者在这种情况下toString 方法@MadProgrammer,取决于口味。 DateTimeFormatter.ISO_LOCAL_DATE 也应该很好用。

标签: java date spring-boot yaml swagger-2.0


【解决方案1】:

我认为最好使用“java.time.LocalDate”。 &lt;configuration&gt; 区域中的&lt;dateLibrary&gt; 标记需要位于&lt;configOptions&gt; 标记内。看这个例子:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <id>api-call</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>src/main/resources/search.yaml</inputSpec>
                <language>java</language>
                <configOptions>
                   <dateLibrary>java8-localdatetime</dateLibrary>
                </configOptions>
                <output>${project.basedir}</output>
                <apiPackage>com.api</apiPackage>
                <modelPackage>com.model</modelPackage>
                <invokerPackage>com.client</invokerPackage>
            </configuration>
        </execution>
    </executions>
</plugin>

您可能想要使用其他参数。见this documentation

here 是java 语言的配置选项。

【讨论】:

    【解决方案2】:

    以下配置适用于 java 11。“java11-localdatetime”生成“java.util.Date”而不是“java.time.LocalDate”。所以我不得不使用 'importMappings' 和 'typemappings' 将 'Date' 替换为 'LocalDate'。

    <plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.27</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
                <configuration>
                    <inputSpec>${project.basedir}/api.yaml</inputSpec>
                        <language>spring</language>                         
                        <output>${project.basedir}</output>
                        <modelPackage>com.xxxx.model</modelPackage>
                        <apiPackage>com.xxxx.generated.api</apiPackage>
                        <generateModels>true</generateModels>
                        <generateModelDocumentation>false</generateModelDocumentation>
                        <generateApis>true</generateApis>
                        <generateApiDocumentation>false</generateApiDocumentation>
                        <generateApiTests>false</generateApiTests>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <configOptions>
                            <hideGenerationTimestamp>true</hideGenerationTimestamp>
                            <dateLibrary>java11-localdatetime</dateLibrary>
                        </configOptions>
                        <importMappings>
                            <importMapping>Date=java.time.LocalDate</importMapping>
                        </importMappings>
                        <typeMappings>
                            <typeMapping>Date=LocalDate</typeMapping>
                        </typeMappings>
                </configuration>
        </execution>
    </executions>
    

    【讨论】:

      猜你喜欢
      • 2021-08-05
      • 2016-01-02
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多