【发布时间】: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