Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need org.threeten.bp.LocalDate to return date in format "YYYY-MM-DD"

I am using yaml file to generate classes that will be used to return JSON response.

yaml

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

I am using swagger-codegen-maven-plugin to generate these classes:

<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>

In my code:

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

Result:

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

I need it to return:

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

I also added in application.properties:

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

But this didn't do anything.

like image 997
Angelina Avatar asked Sep 18 '25 17:09

Angelina


2 Answers

I think is best use the 'java.time.LocalDate'. The <dateLibrary> tag in <configuration> area needs to be inside a <configOptions> tag. See this example:

<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>

You may want use other parameters. See this documentation.

And here are the Config Options for java language.

like image 125
Sergio Marcelo C Figueiredo Avatar answered Sep 20 '25 05:09

Sergio Marcelo C Figueiredo


Following configuration works for java 11. 'java11-localdatetime' generates 'java.util.Date' instead of 'java.time.LocalDate'. So I had to use 'importMappings' and 'typemappings' to replace 'Date' with '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>
like image 34
Nandan Avatar answered Sep 20 '25 06:09

Nandan