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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With